how to save variables in a for loop

Dear,
I have a simple "for loop". I want to save every results ("y") in that. Can you please help me with that ?
thanks in advance.
for x=1:1:5
y = x*x
end

 採用された回答

Sam Chak
Sam Chak 2022 年 4 月 16 日

1 投票

for x =1:5
y(x) = x*x;
end
disp(y)

6 件のコメント

ADNAN KIRAL
ADNAN KIRAL 2022 年 4 月 16 日
編集済み: ADNAN KIRAL 2022 年 4 月 16 日
thanks for reply,
ADNAN KIRAL
ADNAN KIRAL 2022 年 4 月 16 日
Hi there,
Just one more question please. If i have a data as a matrix. like this
x=data(:,1)
Can I get that ?
THANKS again.
Sam Chak
Sam Chak 2022 年 4 月 16 日
編集済み: Sam Chak 2022 年 4 月 16 日
Do you mean storing data like this?
for i = 1:5
x(i) = i;
y(i) = x(i)*x(i);
end
data = [x' y']
save xydata.mat
ADNAN KIRAL
ADNAN KIRAL 2022 年 4 月 16 日
thanks for the reply. I have data in a matrix.
the data is attached here. instead of "x" values, I want to use these values.
thanks again.
Sam Chak
Sam Chak 2022 年 4 月 16 日
How about this one?
% data
data = magic(5)
% assign elements extracted from column 1 of data to vector x
x = data(:,1)
% the square operation
y = x.*x
% display input-output array
table = [x y]
ADNAN KIRAL
ADNAN KIRAL 2022 年 4 月 16 日
i guess it is ok. thnaks again @Sam Chak
all the best.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

製品

リリース

R2021a

質問済み:

2022 年 4 月 16 日

コメント済み:

2022 年 4 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by