I want to delete part of a row in an array but it kept returning an error

1 view (last 30 days)
My array Y is 45 x6 and it is like this:
Y =
56 0 0 0 0 0
3 5 3 4 5 6
:
:
I want to delete the zeros so it becomes:
56
3 5 3 4 5 6
I have tried x(1,2:6) = [] but it c\kepr returning error "A null assignment can have only one non-colon index."
Kindly Support
.

Accepted Answer

Star Strider
Star Strider on 13 Dec 2022
This cannot be done with a numeric array, however it can be done with a cell array.
The disadvantage is that it must be kept as a cell array, and the calculations must use cell array functions —
Y = [56 0 0 0 0 0
3 5 3 4 5 6];
Yc = mat2cell(Y, ones(size(Y,1),1), size(Y,2))
Yc = 2×1 cell array
{[56 0 0 0 0 0]} {[ 3 5 3 4 5 6]}
Lv = cellfun(@(x)x~=0, Yc, 'Unif',0)
Lv = 2×1 cell array
{[1 0 0 0 0 0]} {[1 1 1 1 1 1]}
for k = 1:size(Yc,1)
Yc{k} = Yc{k}(Lv{k});
end
Yc
Yc = 2×1 cell array
{[ 56]} {[3 5 3 4 5 6]}
This should automatically scale to larger ‘Y’ arrays.
See the documentation for mat2cell, cellfun and particularly Access Data in Cell Array for relevant details.
.

More Answers (1)

Jan
Jan on 13 Dec 2022
Edited: Jan on 13 Dec 2022
The error message tells you, that the wanted procedure is not possible in Matlab. Matrices and arrays of more dimensions must habe a rectangular shape. This means, that all rows need the same number of elements, as well as all columns must have the same size also. The meaning of the error message is, that you can delete only complete rows or complete columns.
In other words: You cannot create an array which is looking as your wanted:
56
3 5 3 4 5 6
There is no way to support your wish to create an impossible array.

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!