How to Assign vectors to each dynamic variable name?

Firstly I want to create variable names like z1,z2,..,Zn with n decided by the user
I have an array A of size n*n
I want to assign each row of the array A to each variable
Z1 should contain 1st column of A
Z2 must contain 2nd column of A
Can anyone advice how to do this with some simple method.

 Réponse acceptée

Jan
Jan le 25 Sep 2019

4 votes

Don't do this.
Hiding an index in the name of variables is a bad idea. Use arrays instead and real indices. Having a matrix is effecient already. Using A(:, k) is much smarter and faster than creating a bunch of variables called A<k>.
This topic is discussed almost every day and a typicial problem of beginners.

4 commentaires

Vinay Killamsetty
Vinay Killamsetty le 25 Sep 2019
Modifié(e) : Vinay Killamsetty le 25 Sep 2019
Thank you very much for your valuable advice
But, I have to curve fit each column vector assigned to the variable.
I tried to use cftool for curve fitting each column vector of array A. But it didnt allow
So, I thought of assigning each column vector to a variable and the curve fitting it.
Could you please know if any possible solution to curve fit the coulumn vectors of an array individually using cftool
Jan
Jan le 25 Sep 2019
"I tried to use cftool for curve fitting each column vector of array A. But it didnt allow"
Please post the code and the error message. I'm convinced that creating a set of different variables is not necessary.
Matt J
Matt J le 25 Sep 2019
Modifié(e) : Matt J le 25 Sep 2019
"I tried to use cftool for curve fitting each column vector of array A. But it didnt allow"
cftool is just a GUI for the fit command. For your situation, you should just call fit() directly, in a loop,
for i=1:size(A,2)
cfit(i)= fit(x,A(:,i),fitType);
end
I tried to do something and it didn't work... so instead of asking how to do something you're asking how to do something else entirely which will not help one bit with the original problem.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with Curve Fitting Toolbox dans Centre d'aide et File Exchange

Produits

Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by