How to put elements from a for loop into an array

2 views (last 30 days)
I have the following for loop that gives values for an output 'COLm' based on inout P which ranges from 0 to Q (where Q is equal to 9 currently). I need the values of COLm to be stored in a 1xQ array for use in another section. How is it possible to store such answers in this way?
Thanks
for P = 0:Q %state values
format long
imag=imread('Oban.tif');
SubPlotxP = Txx + (P*(Rx1x-Txx)/10);
SubPlotyP = Txy +(P*(Rx1y-Txy)/10);
DIVx = SubPlotxP;
DIVy = SubPlotyP;
SubPlotxPRound = round(DIVx);
SubPlotyPRound = round(DIVy);
format long
Elev1xP = ((SubPlotxP)- 207000);
Elev1yP = 913000-SubPlotyP;
Elev1xPRound = round(Elev1xP);
Elev1yPRound = round(Elev1yP);
COL = imag(Elev1xPRound,Elev1yPRound,:);
COLm = COL/100;

Accepted Answer

Walter Roberson
Walter Roberson on 7 Feb 2021
COLm(:,P+1) = COL/100;
However: You said that you want a 1xQ array, but you are executing the loop body Q+1 times. Which P value should not have its result recorded?
  7 Comments
Walter Roberson
Walter Roberson on 7 Feb 2021
COLm = zeros(1,Q+1); %before the loop
%...
COL = imag(Elev1xPRound,Elev1yPRound);
COLm(1,P+1) = COL/100; %in the loop
Oscar Zampi
Oscar Zampi on 7 Feb 2021
Thats done the trick.
Thank you so much for your help, it was very useful.

Sign in to comment.

More Answers (0)

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!