desidero scrivere uno script che, data una matrice a, restituisce il nome di ciascuna colonna come segue : a1 = a(: , 1); ecc. ecc.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
vorrei evitare di fare a mano il seguente lavoro, data una matrice a ed il numento di colonne :
a1 = a(:,1); a2=a(:,2); ecc. ecc. ecc. sino ad an=a(:;n)
3 comentarios
Dyuman Joshi
el 24 de Dic. de 2023
"A1 = A(:,1); A2 = A(:,2); A3 = A(:,3); A4 = A(:,4); E COSI' DI SEGUITO SINO AD AN = A(:,N);"
This is exactly what Dynamically naming variables is.
"PER IL MOMENTO QUANDO MI SERVE NOMINARE IN QUESTO MODO LE COLONNE DI UNA MATRICE"
I'll reiterate - Can you specify why do you have to do so?
Respuestas (1)
R
el 3 de En. de 2024
Editada: R
el 4 de En. de 2024
Hi Luigi,
You can achieve the desired functionality by using the "eval" function in MATLAB. Here's an example of the same:
numArrays = 10;
for i = 1:numArrays
eval(['A', int2str(i),' = A(:,i)']);
end
However, it is not recommended to use "eval" in programs. This is also suggested by Dyuman in the comments. Refer to the below link to understand why and see preferred workflows:
An alternative way could be indexing into a cell array. Here is an example:
numArrays = 10;
a = cell(numArrays,1);
for i = 1:numArrays
a{i} = A(:,i);
end
Access the data in the cell array by indexing with curly braces. For example, display the fifth element of A:
a{5}
The assignment statement a{n} = A(:,i) is more elegant and efficient than a call to "eval".
I hope you find this helpful!
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!