give matrix element in for loop

1 view (last 30 days)
HADIMARGO
HADIMARGO on 21 Jun 2021
Answered: Jan on 21 Jun 2021
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.

Answers (1)

Jan
Jan on 21 Jun 2021
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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!