How to create vectors from a cell array?

12 Ansichten (letzte 30 Tage)
Yerzhigit Bapin
Yerzhigit Bapin am 3 Jul. 2017
Kommentiert: Yerzhigit Bapin am 3 Jul. 2017
I have a cell array consisting of 3 cells (1x10, 1x15, 1x20). How can I efficiently create 3 vectors?
  1 Kommentar
Stephen23
Stephen23 am 3 Jul. 2017
Bearbeitet: Stephen23 am 3 Jul. 2017
@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:

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Adam
Adam am 3 Jul. 2017
Bearbeitet: Adam am 3 Jul. 2017
[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 Kommentare
Stephen23
Stephen23 am 3 Jul. 2017
Bearbeitet: Stephen23 am 3 Jul. 2017
cellfun(@cumsum,C,'uni',0)
where C is your input cell array.
Yerzhigit Bapin
Yerzhigit Bapin am 3 Jul. 2017
Thanks, it works!

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 3 Jul. 2017
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};

Community Treasure Hunt

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

Start Hunting!

Translated by