HELP!! I need to use a loop to make my cell array

1 回表示 (過去 30 日間)
Luke Hudgin
Luke Hudgin 2018 年 3 月 3 日
編集済み: Image Analyst 2018 年 3 月 3 日
I have a 40x1 cell array, "locations," where each element is a 270x33 matrix. My goal is a 1x1320 cell array, "master locations," where each element is a 270x1 column vector, which would be all of the individual columns from the elements of "locations." Essentially, I want a code that automates the following
master_location{1} = locations{1}(:,1)
master_location{2} = locations{1}(:,2)
.
.
.
master_location{34} = locations{2}(:,1)
.
.
.
master_location{1320} = locations{40}(:,33)

回答 (2 件)

Image Analyst
Image Analyst 2018 年 3 月 3 日
Try this:
counter = 1;
for k = 1 : 40
for col = 1 : 33
master_location{counter} = locations{k}(:, col);
counter = counter + 1;
end
end
Why does it have to be a cell array rather than a simple, regular numerical array like a double?
  2 件のコメント
Luke Hudgin
Luke Hudgin 2018 年 3 月 3 日
I suppose it could be a double. I was wanting to make it a cell array for the purpose of organization. How could you make it a double?
Image Analyst
Image Analyst 2018 年 3 月 3 日
編集済み: Image Analyst 2018 年 3 月 3 日
Something like
master_location(counter, col) = locations{k}(:, col);
but you need to preallocate space for master_location before the loop. If you need more help, attach a .mat file with your data.
Or maybe what Ahmet gave will work. I like to have data to test with, so attach yours.

サインインしてコメントする。


Ahmet Cecen
Ahmet Cecen 2018 年 3 月 3 日
編集済み: Ahmet Cecen 2018 年 3 月 3 日
Huh, my answer to this question seems to have disappeared.
cat(2,master_location{:})
This should solve your specific problem. The output will be an array instead. If you really want a cell, use mat2cell afterwards.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by