give matrix element in for loop

1 回表示 (過去 30 日間)
HADIMARGO
HADIMARGO 2021 年 6 月 21 日
回答済み: Jan 2021 年 6 月 21 日
hi. i want to make this kind of code in matlab:
a = [ 1 2 3 ];
b1 = 1;
b2 = 3;
b3 = 8;
for i=1:1:3
a(1,i) = b(i)
end
result ==> a = [1 3 8];
how can i create this kind of code in matlab?
pls pay attention i just want to give b1,b2,b3 to a matrix just with "for" loop.

回答 (1 件)

Jan
Jan 2021 年 6 月 21 日
Welcome to Matlab.
This is the most frequently asked question and a severe mistake of beginners. Do not hide indices in the names of variables. This is a shot in your knee and demands for ugly methods to access the variables later on. See this excellent tutorial:
The trivial solution is: Do not create b1, b2, b3, ... but store them in a vector from the beginning.

カテゴリ

Help Center および 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