how to find the correct value of the matrix

1 visualizzazione (ultimi 30 giorni)
anyone help me , when I excute this example , I didn't found in the matrix ''into'' the correct value.
x=[1 1 1 2 2 2 2 3 3 3];
d=0.1:0.1:1;
for ii=1:10
if ii==1:3
into(1:3)=x(1:3)*(d(1));
elseif ii==4:7
into(4:7)=x(4:7)*(d(1));
else
into(8:10)=x(8:10)*(d(1));
end
end

Risposta accettata

Chunru
Chunru il 17 Mar 2022
x=[1 1 1 2 2 2 2 3 3 3];
d=0.1:0.1:1;
into = zeros(size(x));
for ii=1:10
if ii<=3
into(ii)=x(ii)*(d(1));
elseif ii<=7
into(ii)=x(ii)*(d(1));
else
into(ii)=x(ii)*(d(1));
end
end
into
into = 1×10
0.1000 0.1000 0.1000 0.2000 0.2000 0.2000 0.2000 0.3000 0.3000 0.3000

Più risposte (1)

Torsten
Torsten il 17 Mar 2022
Modificato: Torsten il 17 Mar 2022
into = zeros(10,1);
into(1:3)=x(1:3)*d(1);
into(4:7)=x(4:7)*d(1);
into(8:10)=x(8:10)*d(1);
or simply
into = x*d(1);
You can also use your loop, but as
into = zeros(10,1);
for i = 1:10
if i <= 3
into(i) = x(i)*d(1);
elseif i >= 4 & i <= 7
into(i) = x(i)*d(1);
else
into(i) = x(i)*d(1);
end
end
But as said, it's not necessary to make it so difficult.
into = x*d(1)
is the best solution.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by