How to create vectors from a cell array?

I have a cell array consisting of 3 cells (1x10, 1x15, 1x20). How can I efficiently create 3 vectors?

댓글 수: 1

Stephen23
Stephen23 2017년 7월 3일
편집: Stephen23 2017년 7월 3일
@Yerzhigit Bapin: why can't you simply keep the data in the cell array and use indexing? Using indexing is much simpler and more efficient than creating/access variable names dynamically:

댓글을 달려면 로그인하십시오.

답변 (2개)

Adam
Adam 2017년 7월 3일
편집: Adam 2017년 7월 3일

2 개 추천

[a,b,c] = myCell{:};
Not advisable if the number of vectors you want to create escalates to the state that you are next going to ask how you name then a1, a2, a3, etc, but for just extracting an explicit number of outputs the above should work.

댓글 수: 5

Thanks, that's actually was my concern, I'd like my code to be so that I don't need to include vectors manually.
Adam
Adam 2017년 7월 3일
If you do have a variable number of vectors then just keep them in a cell array and access them by index. Or put them into a struct if you really want, with individually named fields.
The latter approach works best where the vectors each relate to something with a meaningful name though rather than stuff you just end up naming a1, a2, a3. e.g. time, velocity, temperature, etc work fine for having independent vectors on a struct, but just arbitrary runs of data work better in a cell array.
ok, so this is what I have so far:
cv = cell(length(P_max),1) ;
for i = 1:length(P_max)
cv{i} = ones(1,P_max(i)) ;
end
I need to create the cumulative sum of all elements of each cell. What I've tried so far creates only one vector.
Stephen23
Stephen23 2017년 7월 3일
편집: Stephen23 2017년 7월 3일
cellfun(@cumsum,C,'uni',0)
where C is your input cell array.
Thanks, it works!

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2017년 7월 3일

1 개 추천

Use deal():
% Setup: Make cell array consisting of 3 cells (1x10, 1x15, 1x20).
ca = {ones(1,10), rand(1,15), randi(9, 1, 20)}
celldisp(ca); % Display it.
% Make vectors
[v1, v2, v3] = deal(ca{:})
or simply assign the 3 vectors
v1 = ca{1};
v2 = ca{2};
v3 = ca{3};

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2017년 7월 3일

댓글:

2017년 7월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by