plot intermediate value in a for statement

Hello,
I have a for statement , with n= 1:1024.
I would like to plot intermediate value result at 64, and 256. instead of doing 3 for statement (1:64, 1:256, 1:1024),
how Can I plot 3 curves with a single for?
I write my code to let you understand better.
n=[1:1024];
d1range=10:100
for k = 1:length(d1range)
.
.
.
.
for j = 1:length(n)
LRIS(k)=LRIS(k)+((sqrt((1/(Lsrpw02(k)*Lrdpw08(k))))));
%conversione in DB
end
LRIS2(k)=LRIS(k)^(-2);
LRISDB(k)=pow2db( LRIS2(k));
end
hold on; box on;
plot(d1range,LRISDB(:,1),'b-o','LineWidth',1);

11 Comments

Please apply the methods to format the code to improve the readability. Thanks.
This loop is strange:
for j = 1:length(n)
LRIS(k)=LRIS(k)+((sqrt((1/(Lsrpw02(k)*Lrdpw08(k))))));
%conversione in DB
end
This adds the same value 1024 times, because the loop body does not depend on the loop counter j. By the way, avoid unnecessary parentheses to improve the readability:
LRIS(k) = LRIS(k) + 1 / sqrt(Lsrpw02(k) * Lrdpw08(k));
The actual question is not clear to me: "plot intermediate value result at 64, and 256". Which intermediate values? At 64 what?
Hello, thaks for your answer.
When I said "plot intermediate value result at 64, and 256" I mean that I want to plot the value of LRIS(k) at 64, at 256 and at 1024.
Infact what I what is to add the same value 1024 times, and plot the value at 64 , and at 256.
If you want to add the value 1024 time, simply write:
LRIS(k) = LRIS(k) + 1024 / sqrt(Lsrpw02(k) * Lrdpw08(k));
without a loop.
Maybe the dots hide important parts of your code, but as far as I can see, the loop over k is not required also. Then running the code for 64, 256 and 1024 elements should be easy.
I am sorry , but that was not the point.
Maybe It's my fault, probably I was not able to explain what I need, thank you for your time.
You are welcome. It is not trivial to explain a problem, because it is a problem. It all details are clear to you already, it would not be a problem anymore. So feel encouraged to explain it again until you get the solution you need.
Ok I'll try again thanks.
I need both for loop, because k values refers to a distance that should be my "first end value", j loop allow me to repeate calculation as a summatory.
What I need is the summatory for 1024, but also for 256 and 64.
I want to exploit my loop for j, to take also the value at 64 , at 256 at 1024 in a "single shot" to be simply, and to plot them in a single plotting.
I hope now I am a bit clearer, Really thank you :)
LRIS(k) = LRIS(k) + 1 / sqrt(Lsrpw02(k) * Lrdpw08(k));
if ismember(k, [64 256 1024])
do whatever plotting is appropriate
end
First, thank you for your answer.
Interesting solution, easier than "If j=64 && j==256 && 1024" but with the same result, infact in the final plot that I have is only j=1024 plotting.
I would like in a sigle plot, the result for j=64, j=256, j=1024 and I am not able to understand how I can do.
So to be simple
L(k) for 64 times --> plot
L(k) for 256 times--> plot
L(k) for 1024 times--> plot
in the same figure, as a comparison.
Thank you
I do not understand what "L(k) for 64 times" mean. But as Walter I assume, that this is the solution:
if any(k == [64, 256, 1024])
plot()
end
If you want something to happen, when k==64, let it happen inside the if branch, not after the loops are finished.
LRIS(k) = LRIS(k) + 1 / sqrt(Lsrpw02(k) * Lrdpw08(k));
if ismember(k, [64 256 1024])
do whatever plotting is appropriate
hold on
end
If j=64 && j==256 && 1024
To use that form of code you would instead need
if j == 64 || j == 256 || j == 1024

Sign in to comment.

Answers (1)

thank you all for your help, you have been really kind! :)

Categories

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

Asked:

on 9 Jan 2021

Answered:

on 10 Jan 2021

Community Treasure Hunt

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

Start Hunting!