How to assign different values to different variables at once using deal() function?
Mostra commenti meno recenti
I had some values which are in a single vector named "param". Let us consider it has "n" elements. I want to assign this "n" values to "n" different variables. I'm using deal() function to do this, but I'm not getting the results as I expected.
[a1,a2,a3,..........., an] = deal(param)
I want values to be assigned in this way
a1 = param(1); a2 = param(2) .... , an = param(n)
can someone help me in this regard?
param = [1 2 3 4 5];
[a1,a2,a3,a4,a5] = deal(param)
3 Commenti
Nikhil
il 28 Giu 2023
[B1,...,Bn] = deal(A) copies the single input argument A and returns it as the output arguments B1,...,Bn. It is the same as B1 = A, …, Bn = A. In this syntax, you can specify an arbitrary number of output arguments.
That's why the array param is getting assigned to a1,a2,..,a5.
Dyuman Joshi
il 28 Giu 2023
Any particular reason why you want to do this assignment?
Steven Lord
il 28 Giu 2023
Can you dynamically create variables with numbered names like x1, x2, x3, etc.? Yes.
Should you do this? The general consensus is no. That Answers post explains why this is generally discouraged and offers several alternative approaches.
If the vector you were trying to assign to those variables had 1000, 10000, or more elements do you really want 1000, 10000, or more individual variables in the workspace? Probably not. Instead of assigning to and using (for example) a523 why not just use param(523)?
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!