How to create a new variable within each run in a for loop?

31 visualizzazioni (ultimi 30 giorni)
For an assignment of mine, I am using a for loop to obtain a new matrix "A" for each number within an "x" range.
However, every time the loop runs, the variable A remains unchanged, and so this would make it difficult to distinguish
the matrix with different numbers of "x". Is there a way where each time the loop runs, I can set a new variable equal to "A" continuously?
So then I would get A1, A2, A3, A4, A5...etc. I would appreciate any help!
clc
clear all
%define variables
x=0:0.01:2;
for k=1:length(x) %x as a range
alpha=atan(1./(2-x(k))); %alpha angle
beta=atan((2-x(k))./1); %beta angle
gamma=atan(x(k)); %gamma angle
A=zeros(10); % setting up zero matrices
b=[0;2;0;1;0;0;0;0;0;0]; % for later substitution
%Define the A matrix
A(1,1:2)=[-1, cos(alpha)];
A(2,2)=[sin(alpha)];
A(3,1:5)=[1,0,0,0,-1];
A(4,3)=[-1];
A(5,2:7)=[sin(beta),0,0,0,sin(gamma),-1];
A(6,2:5)=[-cos(beta),-1,0,-cos(gamma)];
A(7,6:8)=[1,0,-1];
A(8,7:9)=[-1,0,1];
A(9,4:10)=[1,sin(gamma),0,0,0,0,-1];
A(10,5:7)=[cos(gamma),0,1];
A
xrow=pinv(A)*b %gaussian method for finding unknown forces
end
  4 Commenti
Jason Wang
Jason Wang il 9 Lug 2021
I have added my code
I apologize for not adding so earlier
Stephen23
Stephen23 il 9 Lug 2021
Modificato: Stephen23 il 9 Lug 2021
"However, every time the loop runs, the variable A remains unchanged"
Because you have not used any indexing based on the loop iteration variable k to store the results of each loop. How to use indexing in a loop is shown in the introductory tutorials, or the documentation on array preallocation:
"Is there a way where each time the loop runs, I can set a new variable equal to "A" continuously?"
Of course, this is easy with basic MATLAB indexing.
"So then I would get A1, A2, A3, A4, A5...etc."
That would be about the worst approach.
Rather than forcing pseudo-indices into variable names, just use actual indices in to one array.

Accedi per commentare.

Risposta accettata

DGM
DGM il 8 Lug 2021
Modificato: DGM il 9 Lug 2021
Arrays are indexable. Scalars and variable names aren't. If you find yourself embedding ordinal information in the variable names, you should probably stop and use an array.
Just store the variables in an array of some sort suitable to the type of things you're putting in it.
For example:
N = 4;
bigA = cell(N,1);
for n = 1:N
A = rand(4);
bigA{n} = A;
end
format compact % just for tighter web display
celldisp(bigA)
bigA{1} = 0.1840 0.7896 0.8760 0.8886 0.2737 0.9784 0.5529 0.7511 0.0631 0.5860 0.6735 0.9561 0.5812 0.2218 0.1747 0.3220 bigA{2} = 0.5867 0.8644 0.8288 0.7139 0.1717 0.1442 0.6000 0.7751 0.2383 0.9257 0.1120 0.9512 0.2443 0.7263 0.5976 0.5300 bigA{3} = 0.9682 0.1485 0.0110 0.7669 0.2349 0.3334 0.2995 0.8681 0.8535 0.8289 0.3777 0.8126 0.4849 0.6876 0.8785 0.3668 bigA{4} = 0.3385 0.6856 0.0589 0.3541 0.7796 0.4151 0.1089 0.9215 0.0120 0.2100 0.3975 0.1568 0.7010 0.5085 0.3946 0.0702
Using a cell has some advantages if the matrix size varies or is extremely large, but a ND array works and may be easier to use in some case.
It's also possible that the whole issue of storage could be avoided by simply calculating the appropriate matrix at the point in the code where it needs to be used. To know whether that would be an option would require further insight into your task and the code you've written so far.
EDIT:
Not really knowing what you're going to do with the results, I'm assuming you want to keep both A and xrow:
x=0:0.01:2;
results = cell(numel(x),2);
for k=1:length(x) %x as a range
alpha=atan(1./(2-x(k))); %alpha angle
beta=atan((2-x(k))./1); %beta angle
gamma=atan(x(k)); %gamma angle
A=zeros(10); % setting up zero matrices
b=[0;2;0;1;0;0;0;0;0;0]; % for later substitution
%Define the A matrix
A(1,1:2)=[-1, cos(alpha)];
A(2,2)=[sin(alpha)];
A(3,1:5)=[1,0,0,0,-1];
A(4,3)=[-1];
A(5,2:7)=[sin(beta),0,0,0,sin(gamma),-1];
A(6,2:5)=[-cos(beta),-1,0,-cos(gamma)];
A(7,6:8)=[1,0,-1];
A(8,7:9)=[-1,0,1];
A(9,4:10)=[1,sin(gamma),0,0,0,0,-1];
A(10,5:7)=[cos(gamma),0,1];
xrow=pinv(A)*b; %gaussian method for finding unknown forces
results{k,1} = A;
results{k,2} = xrow;
end
This stores A and xrow for each point in the parameter sweep. Again, the same could be done with plain numeric arrays by stacking A on dim3 and side-concatenating xrow on dim2 or so.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by