Can matlab assign a vector of objects?

5 Ansichten (letzte 30 Tage)
Jacob Thompson
Jacob Thompson am 6 Apr. 2020
Kommentiert: Tommy am 6 Apr. 2020
[x,y,z] = [1,2,3]
Say I want to assign the objcts x, y, and z to values 1,2,3
Can I create a vector of objects [x,y,z] and assign it [1,2,3]? Why does
[x,y,z] = [1,2,3] not work? Surely I dont have to laboriously type out
x = 1
y = 2
z = 3
yes?

Antworten (1)

Tommy
Tommy am 6 Apr. 2020
One possibility...
C = num2cell([1 2 3]);
[x,y,z] = C{:};
  2 Kommentare
Jacob Thompson
Jacob Thompson am 6 Apr. 2020
Is there an inverse operation that can undo num2cell?
Tommy
Tommy am 6 Apr. 2020
Yes, cell2mat:
>> num2cell([1 2 3])
ans =
1×3 cell array
{[1]} {[2]} {[3]}
>> cell2mat(ans)
ans =
1 2 3

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Construct and Work with Object Arrays finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by