save values in array in a loop

576 ビュー (過去 30 日間)
Sana Tayyeb
Sana Tayyeb 2015 年 3 月 21 日
回答済み: Anil Kumar 2022 年 6 月 22 日
I want to save a value calculated inside a loop in an array. Is it possible? for example:
for i=1:10
s=a+b;
a(i)=s;
end
a
ans=
a=[23 45 678 21 34 134 34 56 11 34]
how to save s in an array so that this loop give an array containing the calculated values?
  3 件のコメント
Sana Tayyeb
Sana Tayyeb 2015 年 3 月 22 日
this loop is caluculation meanGL. after loop ends i can only access meanGL of last spot .i want to save meanGL of all spots in a way that after calculation i can access meanGL of all spots.
for k = 1 : numberOfSpots
thisSpotPixels = SpotMeasurements(k).PixelIdxList; % Get list of pixels in current blob.
meanGL = mean(i(thisSpotPixels)); % Find mean intensity
end
Thanks in advance
Debasish Roy
Debasish Roy 2018 年 2 月 3 日
first of all make a script like this, then run it. You will get your answer i hope.
clear all clc
x = []; y = []; k =[]; f = []; for i= 1:4 fprintf('Enter the temperature of day %d \n' ,i ); s = input(' in celcius '); x(i) = s; y(i) = i; end k = x+273; f = x*9/5 + 32; plot( y, x , '-R', y , f ,'-G' ,y ,k, ' -B')

サインインしてコメントする。

回答 (2 件)

Konstantinos Sofos
Konstantinos Sofos 2015 年 3 月 21 日
x = [] % initialize your matrix/vector
for i=1:10
% do something useful
s=a+b
x = [x,s];
end
  3 件のコメント
Konstantinos Sofos
Konstantinos Sofos 2015 年 3 月 21 日
More or less I agree with you...but here we do not have any performance issue question...just a way to keep the values ;)
hemasai proddutur
hemasai proddutur 2021 年 7 月 7 日
d = 0;
for t=1:10000
d = d+0.1;
end
i want to store the values in array in the loop [0.1 -----------------1000]

サインインしてコメントする。


Anil Kumar
Anil Kumar 2022 年 6 月 22 日
may be helpful for begginers
x1=[];
x2=[];
a=[2 4 6 7 3];
b=[1 9 7 5 8];
for ii=1:5
s=a(ii)+b(ii);
x1=[x1,s];%output in a row
x2=[x2;s];%output in a column
end

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by