Converting a cell array into a matrix of stipulated number of columns and rows
조회 수: 2 (최근 30일)
이전 댓글 표시
Consider I have a cell array containing 4 cells
Each cell containd 'n' elements
Now I want to covert it into an array of size "n*4" where element of each row is take from elements of each cell
If it has tto be more clear for eg: ist row of the matrix contains 4 elements - each element is picked from 1st element of each cell.
I have used the model
for i = 1:numel(W{1})%w is the cell
wid(i,:)=double(cellfun(@(v) v(i),W));
end
But it is taking more time in matlab.So, is there any best and simple solution for this problem which can be simulated in less time?
댓글 수: 2
Fangjun Jiang
2019년 12월 12일
The code seems unnecessary. Please construct an example with n=2 and explain what is the input and desired output.
채택된 답변
Andrei Bobrov
2019년 12월 12일
편집: Andrei Bobrov
2019년 12월 12일
Let C - your cell array.
if cells sizes are the same:
out = reshape(cat(2,C{:}),[],4);
if not the same:
out = cell2mat(cellfun(@(x)x(:),C,'un',0));
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!