nested Loop with complicated conditions

Hi every1, i need your help please. i am trying to use nested For-Loop for my problem but i am not sure if my code is correct. I have a vektor of nX128 (n can be any number of rows), i have to calculate a certain value (F) form the first row 1X128(from the first Loop in my code), and use (F) to calculate (Z) and (R) in next loop 2X128. And for the next loop i have to use the previous value of (F) to calculate (Z)and (R) till the end of n. As a result i want the last value of (R), i mean from the last nX128. It is quite complicated so i calculate the values of (F) and (R) with excel to have an idea in which rang of values they have to be. Comparing to the result of excel, the (R)-Value is different and very very larg from this code.
Rp=1; % i have to give a value for the first (R) to beginn the calculation
for j = 1:size(Pulses,1) % the first Loop with vector named Pulses = [1X128]
if j == 1
Z(j,:)= calculateCPW(Pulses(j,:),SBP,DBP,Fs); % Function to calculate (Z)
F(j,:) = CalcRootFlow(Z(j,:),MAD,Pulses(j,:),Rp); % anther fuction to calculate (F) from the first (Z) and
the first Value of (R=Rp=1)
end
for j = 1:size(Pulses,1)% the next Loop
if j >1 % hier should beginn with Pulses=[2X128]
Z(j,:)= calculateCPW(Pulses(j,:),SBP,DBP,Fs); % again the Function to calculate (Z) from [2X128]
M1 = mean(F(j-1,:)); % the mean of the previous value of (F) ( for the 2.Loop shloud be from the 1.Loop and
for upto the 3.Loop the previos Value)
M2 = mean(Z(j,:));% the mean of (Z) in this Loop
R(j,:)=M2/M1; % calculation of (R)
F(j,:) = CalcRootFlow(Z(j,:),MAD,Pulses(j,:),R(j,:)); % again the Function to calculate (F) to use it for
[3X128] and so on....
end
end %End of the next Loop
end %End of the first Loop
R = R(size(NormedPulses,1),:); % hier i want to see all n values of (R)

3 Comments

Dennis
Dennis on 30 Jul 2018
Edited: Dennis on 30 Jul 2018
Why do you need 2 loops? Your first loop does some calculation for j==1 (first iteration), your 2nd loop runs several times to calculate values for j>1.
Using 'j' for both loops also might (i don't think its the case here) lead to strange results.
Thank you Dennis for your Answear:) I tried with one Loop, i have also the very very large value of (R). Also i noticed that, by j>1 begins with j=1 too, what i do not want to. Do you think that problem is with if?
I just wanted to write a 1-loop solution, however i realised that you wrote that you use F to calculate R and Z,but i am not sure if you implemented that - atleast the calculation of Z does not use F.
Rp=1; % i have to give a value for the first (R) to beginn the calculation
for j = 1:size(Pulses,1) % the first Loop with vector named Pulses = [1X128]
if j == 1
Z(j,:)= calculateCPW(Pulses(j,:),SBP,DBP,Fs); % Function to calculate (Z)
F(j,:) = CalcRootFlow(Z(j,:),MAD,Pulses(j,:),Rp);
else
Z(j,:)= calculateCPW(Pulses(j,:),SBP,DBP,Fs);
^is this supposed to be F(j-1,:) ?
M1 = mean(F(j-1,:));
M2 = mean(Z(j,:));% the mean of (Z) in this Loop
R(j)=M2/M1; % calculation of (R)
F(j,:) = CalcRootFlow(Z(j,:),MAD,Pulses(j,:),R(j));
end
end %End of the first Loop
R(:) % hier i want to see all n values of (R)

Sign in to comment.

Answers (0)

Categories

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

Asked:

on 30 Jul 2018

Commented:

on 30 Jul 2018

Community Treasure Hunt

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

Start Hunting!