Create a vector name from two integer variables
Mostrar comentarios más antiguos
Hi,
I want to create a vector name from two integer variables. For example I have two for loops
for i=1:N for j=1:M
and I want to create a vector called vectorij, i.e: vector12, vector 13 and so on. Thanks
3 comentarios
Traian Preda
el 9 de Sept. de 2013
Jan
el 9 de Sept. de 2013
I do not understand, what you are looking for. Could you provide an explicit example with real data?
Traian Preda
el 9 de Sept. de 2013
Respuestas (3)
James Tursa
el 9 de Sept. de 2013
2 votos
2 comentarios
Azzi Abdelmalek
el 9 de Sept. de 2013
Editada: Azzi Abdelmalek
el 9 de Sept. de 2013
I think he is not asking for variables name, just a cell array of stings
Jan
el 9 de Sept. de 2013
No, "vector11=[1,5]" from the comment looks, like names are wanted.
Hiding the indices inside the names of variables is really cruel. Don't do this. You'd never do this in the real life. Names are names, data are data.
A{1} = [1 2 3 4; 5 6 7 8];
A{2} = [3 5 3 4; 6 6 7 9];
A{3} = [3 2 3 5; 1 6 4 8];
Now write instead of "vector11":
A{1}(:, 1)
Or general instead of "vector_ij_":
A{i}(:, j)
This is fast, easy to expand to billions of iterations and fast to process. Using a 3D-array might be even faster, if the elements of A have all the same size.
Azzi Abdelmalek
el 9 de Sept. de 2013
Editada: Azzi Abdelmalek
el 9 de Sept. de 2013
This is a vector of names
out={};
for k=1:5
for p=1:5
out{end+1}=sprintf('vector%d%d',k,p)
end
end
3 comentarios
Sean de Wolski
el 9 de Sept. de 2013
regexp(sprintf('vector%i%i\nS',fullfact([5 5])'),'S','split')'
:)
Bajdar Nour
el 21 de Ag. de 2018
What is mean by something like this name= []?
Image Analyst
el 21 de Ag. de 2018
It sets the variable called "name" to null, or empty. It still exists, it's just that it has no value at all. Doing that to a row of a matrix will delete that row from the matrix, shortening it.
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!