I need to get all of the outputs of b into a row vector size [1 5] so b=[4,1,0,1,4]
for a=-2:2
b=a.^2
end
Outputs are
b=4
b=1
b=0
b=1
b=4
I tried
for a=-2:2
b(a)=a.^2
end
I got, Array indices must be positive integers or logical values. So then I tried
for a=-2:2
b(1:a)=a.^2
end
But I did not get what I wanted.

1 Kommentar

VBBV
VBBV am 1 Okt. 2021
Bearbeitet: VBBV am 1 Okt. 2021
x = 1;
for a=-2:2
b(x)=a^2;
x = x+1;
end
b
b = 1×5
4 1 0 1 4
Try this, But as @KSSV suggested, no loop needed

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

KSSV
KSSV am 1 Okt. 2021

1 Stimme

a = -2:2 ;
for i = 1:length(a)
b(i)=a(i).^2
end
No Loop needed:
a = -2:2 ;
b = a.^2 ;

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2020b

Gefragt:

am 1 Okt. 2021

Bearbeitet:

am 1 Okt. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by