フィルターのクリア

Use of CELLFUN instead of FOR loop

8 ビュー (過去 30 日間)
Neil
Neil 2013 年 6 月 9 日
I have a 21x40 cell array with each cell containing a vector of variable length. I want to merge all of the cells in each column. I can achieve this using a FOR loop such as
A = 21x40 cell array
for n = 1:40
merge = cat(2,A{:,n})
end
How can I do this with, for example, CELLFUN or in some other way without using a FOR loop? Thanks a lot for any help.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 9 日
編集済み: Azzi Abdelmalek 2013 年 6 月 9 日
A=num2cell(rand(21,40)) % Example
out=arrayfun(@(x) cat(2,A{:,x}),1:size(A,2),'un',0)
  2 件のコメント
Neil
Neil 2013 年 6 月 10 日
Thanks! I tried something similar with cellfun but that didn't work.
Jan
Jan 2013 年 6 月 10 日
This is a fast and short method. The "merge{n}= cat(2,A{:,n})" approach inside a loop is 50% faster.

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 9 日
for n = 1:40
merge = cat(2,A{:,n})
end
is the same then
merge = cat(2,A{:,40})
  4 件のコメント
Neil
Neil 2013 年 6 月 9 日
The for loop in my code does this on a per column basis. As I understand your code it only works on a single column. Am I missing something about your code?
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 9 日
編集済み: Azzi Abdelmalek 2013 年 6 月 9 日
Your for loop should be: (merge{n} instead of merge)
for n = 1:40
merge{n}= cat(2,A{:,n})
end

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

カテゴリ

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