cell array indexing oddity

i have a large cell array of type cell
when i do this
test{2:2:end,7} i get back cells
when i do this i get back ints that are in the cell
test{1,1}
Its very frustrating, I wanted to access all the rows from 2 to the end skipping one in the midding of col 7
Why is that so hard?
It works for a single instance but cant do it in a vectorized form

6 个评论

Could you confirm that you want 2:2:end, every other row "skipping one each time", and not 2:end (every row skipping one row, namely row 1) ?
imported_data{1,1} is not necessarily the same datatype as the other entries in the cell.
imported_data{2:2:end,7}
Could you confirm that is what you are using? If there are at least 4 rows, that would be "cell expansion", resulting in a "comma separated list" of values, rather than a single value. You would often then need to capture the values somehow, such as {imported_data{2:2:end,7}} or vertcat(imported_data{2:2:end,7}) or horzcat(imported_data{2:2:end,7})
i am using exactly that
imported_data{2:2:end,7}
You're going to have to demonstrate a working example of your problem.
A = num2cell(reshape(1:70,10,[]))
A = 10×7 cell array
{[ 1]} {[11]} {[21]} {[31]} {[41]} {[51]} {[61]} {[ 2]} {[12]} {[22]} {[32]} {[42]} {[52]} {[62]} {[ 3]} {[13]} {[23]} {[33]} {[43]} {[53]} {[63]} {[ 4]} {[14]} {[24]} {[34]} {[44]} {[54]} {[64]} {[ 5]} {[15]} {[25]} {[35]} {[45]} {[55]} {[65]} {[ 6]} {[16]} {[26]} {[36]} {[46]} {[56]} {[66]} {[ 7]} {[17]} {[27]} {[37]} {[47]} {[57]} {[67]} {[ 8]} {[18]} {[28]} {[38]} {[48]} {[58]} {[68]} {[ 9]} {[19]} {[29]} {[39]} {[49]} {[59]} {[69]} {[10]} {[20]} {[30]} {[40]} {[50]} {[60]} {[70]}
vertcat(A{2:2:end,7})
ans = 5×1
62 64 66 68 70
i am trying to multiple the values in that cell array by a scalar and have tried just about every trick in the book
dot operator
cell2mat
i literally cant get anything to work
I have opened the data and verified line by line by line by line that they are all the same type yet cell2mat wont work either
nor can i multiple the thing by a scalar
K>> test{2:2:end,7}*1
Error using *
Too many input arguments.
A = num2cell(reshape(1:70,10,[]))
A = 10×7 cell array
{[ 1]} {[11]} {[21]} {[31]} {[41]} {[51]} {[61]} {[ 2]} {[12]} {[22]} {[32]} {[42]} {[52]} {[62]} {[ 3]} {[13]} {[23]} {[33]} {[43]} {[53]} {[63]} {[ 4]} {[14]} {[24]} {[34]} {[44]} {[54]} {[64]} {[ 5]} {[15]} {[25]} {[35]} {[45]} {[55]} {[65]} {[ 6]} {[16]} {[26]} {[36]} {[46]} {[56]} {[66]} {[ 7]} {[17]} {[27]} {[37]} {[47]} {[57]} {[67]} {[ 8]} {[18]} {[28]} {[38]} {[48]} {[58]} {[68]} {[ 9]} {[19]} {[29]} {[39]} {[49]} {[59]} {[69]} {[10]} {[20]} {[30]} {[40]} {[50]} {[60]} {[70]}
A{2:2:end,7} % output is multiple scalars
ans = 62
ans = 64
ans = 66
ans = 68
ans = 70
vertcat(A{2:2:end,7}) % output is a single column vector
ans = 5×1
62 64 66 68 70
You need to deal with the fact that that expression has multiple outputs.

请先登录,再进行评论。

回答(1 个)

I know you said you tried using cell2mat(), but you must have not used it correctly. Try using cell2mat() like this:
test = num2cell(reshape(1:80,10,[])) % 10 rows by 8 columns
% Take contents of 7th column and even numbered rows.
% 7th column has 10 elements.
out = cell2mat(test(:,7)); % Get 7th column.
out = out(2:2:end) % Every other element to give 5 elements.
whos test
whos out

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

产品

版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by