How to extract every elements with different dimension at multiple cells in efficient manner?
이전 댓글 표시
Dear all,
Say I have three cells, where the element
dd = [49;50;51;52;53;54] [55;56;57] [85;86;87;88;89;90]
The objective is to extract all the element in each cell and make it into single row, such that
[40 50 51 52 53 54 55 56 57 85 86 87 88 89 90]
I used the following lines.
C =1
for i =1:size(dd,2)
Xcx = cell2mat (dd (1,i));
ee =[]
ee (1,1: length (Xcx)) =Xcx;
for k =1: length (ee )
new (C,1) = ee (k)
C =C+1
end
end
Even though the code do the trick, I am looking if there is more advance and compact ways of doing it?
Thanks in advance
채택된 답변
추가 답변 (2개)
Sanjay Nair
2017년 8월 2일
Hello,
If I'm understanding your problem correctly you have a cell array that can be defined in the following manner
dd = {[49;50;51;52;53;54], [55;56;57], [85;86;87;88;89;90]};
Since you have a cell array of column vectors with only three elements, perhaps the easiest way to extract them into a single row vector would be to transpose and concatenate the contents in a single command as follows:
row = [dd{1}', dd{2}', dd{3}'];
You can get more information about accessing data in cell-arrays in: https://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-cell-array.html
Akira Agata
2017년 8월 3일
How about using cellfun and horzcat functions as follows. In this code, each element in cell array dd is transposed by cellfun. Next, horzcat concatenate each element horizontally.
dd = {[49;50;51;52;53;54], [55;56;57], [85;86;87;88;89;90]};
ddTrans = cellfun(@transpose,dd,'UniformOutput',false);
Output = horzcat(ddTrans{:});
카테고리
도움말 센터 및 File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!