desidero scrivere uno script che, data una matrice a, restituisce il nome di ciascuna colonna come segue : a1 = a(: , 1); ecc. ecc.
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Dyuman Joshi
2023-12-24
"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?
回答(1 个)
R
2024-1-3
编辑:R
2024-1-4
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!