How to put elements from a for loop into an array

조회 수: 3 (최근 30일)
Oscar Zampi
Oscar Zampi 2021년 2월 7일
댓글: Oscar Zampi 2021년 2월 7일
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;

채택된 답변

Walter Roberson
Walter Roberson 2021년 2월 7일
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
Walter Roberson
Walter Roberson 2021년 2월 7일
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 2021년 2월 7일
Thats done the trick.
Thank you so much for your help, it was very useful.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by