How to put elements from a for loop into an array

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 日

0 投票

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 件のコメント

Oscar Zampi
Oscar Zampi 2021 年 2 月 7 日
HI, Thank you for the reply.
That was my error, it shoukld be a 1xP array.
When running what you suggested, it gives the 1x1, then 1x2, then 1x3, ect, up until the 1xP. Is it possible to produce only the 1xP?
Walter Roberson
Walter Roberson 2021 年 2 月 7 日
I am confused. Your P is a for loop, so it is changing magnitude all of the time, so if the output should be 1 x P then it should be increasing in size, just as it is.
And you have not answered the question of which of the Q+1 iterations of P=0:Q that you want to have the output discarded for.
It is difficult to generalize from your code, as you are doing the same thing for every iteration of P, except for wanting to output into different columns.
Perhaps what you want is:
COLm = zeros(3,Q+1); %before the loop
before the loop.
Or maybe you are wanting
COLm = zeros(Q+1, 3); %before the loop
and
COLm(P+1,:) = COL/100; %in the loop
Oscar Zampi
Oscar Zampi 2021 年 2 月 7 日
Hi,
My apologies if I am not explaining it too well, I am still very new to MATLAB.
What i require is a vector of size 1xQ+1 (I went over the code again and realised that the size requirement was different). This vector will be used in another section of the code that uses each component of the vecotr for differeing calculations. This other section does not allow for all vectors (1x1 through to 1xQ), and as a result is gives out errors. Hence, just the 1xQ+1 is wanted.
Is there any way to extract only this needed part?
Thanks again
Walter Roberson
Walter Roberson 2021 年 2 月 7 日
imag=imread('Oban.tif');
That is typically going to be height by width by 3, with the 3 representing RGB. But with tif files, it could be x 1 (Grayscale) or x 2 (Grayscale + Alpha) or x 4 (RGB + Alpha, or CMYK) . And that's not even counting if you have ExtraSamples
COL = imag(Elev1xPRound,Elev1yPRound,:);
That would extract all 3 (or 1, 2, or 4) color panes.
So, typically COL would be 1 x 1 x 3, assuming that Elev1xPRound and Elev1yPRound are scalars.
Is your TIF known to be grayscale? If so then COL = imag(Elev1xPRound,Elev1yPRound); would make it clear that only one pane was being extracted.
If it is RGB then you would be extracting 3 x (Q+1) . But if the other functions are only expecting 1 x (Q+1) or 1 x Q, then which of the 3 color panes do you want?
Oscar Zampi
Oscar Zampi 2021 年 2 月 7 日
Hi,
The TIF is greyscale, yes.
The other fucntion is not directly related to the colour of the image; in fact the colour represents elevation levels. The colour value is needed since it represents a height to be used in the afforementioned later calculations.
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 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by