write the results of two for loops with different indexes into an matrix

2 views (last 30 days)
hello everyone. thanks to anyone who can help me. i wrote the code, where we have two for loops (ii and n). i created an array of zero (ii, n). i want the result of etat to be written inside the array. The goal is that for each value of n, the system should perform the inner for loop (making ii vary). write the results into the previously defined array of zeros and then go to n+1 and perform the same operations. at the end of the two loops, the array should contain all the values of etat. thanks to anyone who can help me
(for each value of B, the program should run the internal for loop for all Y values, write the values into the array, and then go to the value n +1 and repeat the same operations )
I cannot get all etat results to write within the Etat matrix. (for each value of n must find 1001 values of etat and write them all into the Etat matrix)
thanks
UT=[B,T0]; % matrix already defined 6681*2
for n = size (1 : 6680)
Y=(0:0.1:100); % row vector of 1001 elements
Etat=zeros(1001,6681);
for ii=(1:1000)
ws=(1+rhob)*UT(n,1)/(1+rhob*Y(1,ii));
wr=(((1+rhob)*Y(1,ii))/(1+rhob*Y(1,ii)))*UT(n,1);
h=((ws-UT(n,1))*(wr-UT(n,1)))/(UT(n,1)*(ws-wr));
H=abs(h);
etapg=1-(1-eta0)*H;
Ts=UT(n,2)*((UT(n,1)-wr*etapg)/(etapg*(ws-wr)));
Tr=UT(n,2)*((etapg*ws-UT(n,1))/(etapg*(ws-wr)));
etas=(p00+p10*((ws/wsmax)*a)+p01*((Ts/Tsmax)*b)+p20*(((ws/wsmax)*a)^2)+p11*((ws/wsmax)*a)*((Ts/Tsmax)*b)+p02*(((Ts/Tsmax)*b)^2))/100;
etar=(p00+p10*((wr/wrmax)*a)+p01*((Tr/Trmax)*b)+p20*(((wr/wrmax)*a)^2)+p11*((wr/wrmax)*a)*((Tr/Trmax)*b)+p02*(((Tr/Trmax)*b)^2))/100;
etat=etapg*((Tr*wr+Ts*ws)/((Tr*wr/etar)+(Ts*ws/etas)));
end
end
  3 Comments
Dyuman Joshi
Dyuman Joshi on 16 Feb 2024
In addition to Matt's comment -
for n = size (1 : 6680)
What is this line of code supposed to do?
Note that the output of size for that particular syntax is a row vector.
You might want to use numel.
Alessandro
Alessandro on 16 Feb 2024
the code does not write the correct results into the matrix. the results of one iteration are copied throughout the matrix

Sign in to comment.

Answers (1)

Aquatris
Aquatris on 16 Feb 2024
Edited: Aquatris on 16 Feb 2024
You need to provide more info for a better help. However here is my attempt with the limited info.
There are a couple reasons why you are not getting what you want:
for n = size (1 : 6680) % this is wrong as it is equaivalent to "for n = [1 6680]"
% which does not loop at all and has only 2
% iteration, so your outer loop only runs twice
for n = 1 : 6680 % this one makes n loop through 1 to 6680
and
Etat(ii,1:n+1) = double (etat); % this is trying to set all the rows at once
% but I assume etat is a scalar meant
% for individual elements
Etat(ii,n+1) = double (etat); % this one will assing a single element in Etat to etat

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!