Use of CELLFUN instead of FOR loop
조회 수: 8 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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
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
2013년 6월 9일
for n = 1:40
merge = cat(2,A{:,n})
end
is the same then
merge = cat(2,A{:,40})
댓글 수: 4
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 Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!