Need Help Indexing. I have a 1x12 cell array with each cell containing a 120x2 double. I need to use a for loop to index all the values in every of row in the second column of the 120x2 double and store them in their own 1x12 matrix.

4 views (last 30 days)
Cool = Matrix(:,2);
[row,col] = size(Cool);
N = 1;
M = 1;
for N = 1:1:col
for M = 1:1:row
Cool_Temp{M} = Cool{M}(:,2);
end
end

Answers (1)

Peng Li
Peng Li on 3 Apr 2020
Do you necessarily do with a for loop? If not, you don't need one.
% an example
tst = cell([1, 12]);
tst(:) = {randn(120, 2)};
% extract the second column of each cell element and store them in another cell
res = cellfun(@(x) x(:, 2), tst, 'UniformOutput', 0);
>> res
res =
1×12 cell array
Columns 1 through 6
{120×1 double} {120×1 double} {120×1 double} {120×1 double} {120×1 double} {120×1 double}
Columns 7 through 12
{120×1 double} {120×1 double} {120×1 double} {120×1 double} {120×1 double} {120×1 double}

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by