Assigning multiple variables vectors from a cell array

24 Ansichten (letzte 30 Tage)
Johannes Berger
Johannes Berger am 13 Dez. 2017
Bearbeitet: Jan am 14 Dez. 2017
I'm wondering if there is any smart solution on how to access multiple same-sized vectors inside of a cell array and assign them to an independent variable.
cellArray =
1×4 cell array
{1×101 double} {1×101 double} {1×101 double} {1×101 double}
Thanks!

Akzeptierte Antwort

Honglei Chen
Honglei Chen am 13 Dez. 2017
Bearbeitet: Honglei Chen am 13 Dez. 2017
I would just do the following and then access them using 3rd indices
reshape(cell2mat(cellArray),[size(cellArray{1}) length(cellArray)])
HTH
  2 Kommentare
Johannes Berger
Johannes Berger am 13 Dez. 2017
Thank you! Exactly what I was looking for.
Jos (10584)
Jos (10584) am 13 Dez. 2017
Simple
cat(3, cellArray{:})
would do ...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Adam
Adam am 13 Dez. 2017
[a1, a2, a3, a4] = cellArray{:};
It is not advisable generally to do this, but for the record, that syntax should work. Having 1 variable is almost always a lot better than 4.
  2 Kommentare
Johannes Berger
Johannes Berger am 13 Dez. 2017
Thank you, this solution does work, although one would have to look for a way to dynamically create as much variables as needed since the number of vectors in the cell array varies.
Stephen23
Stephen23 am 14 Dez. 2017
Bearbeitet: Stephen23 am 14 Dez. 2017
"...although one would have to look for a way to dynamically create as much variables as needed since the number of vectors in the cell array varies."
No, that is exactly what one should avoid doing. Magically creating or accessing variables will make your code slow, complex, buggy, obfuscated, hard to debug, and insecure. Read this to know why:
Adam already told you a much better solution, which is to keep your data in one array.

Melden Sie sich an, um zu kommentieren.


Jan
Jan am 13 Dez. 2017
Bearbeitet: Jan am 13 Dez. 2017
Do you mean:
a = cellArray{1}{1};
b = cellArray{2}{1};
c = cellArray{3}{1};
d = cellArray{4}{1};
Now a,b,c,d are 1x101 vectors.
  3 Kommentare
Stephen23
Stephen23 am 14 Dez. 2017
"...but this does not automatically match the varying number of vectors in the cell array."
Magically creating variables would be slow, complex, inefficient, hard to debug, and insecure. Using indexing is simple, neat, easy to debug, and very efficient.
Jan
Jan am 14 Dez. 2017
Bearbeitet: Jan am 14 Dez. 2017
@Johannes: Your question looks, like you have a nested cell:
cellArray =
1×4 cell array
{1×101 double} {1×101 double} {1×101 double} {1×101 double}
Therefore I've added the {k}{1}. As Stephen has pointed out: Such an automagical creation of variables is a bad programming pattern, which is known to cause more troubles than it solves.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by