how to find the correct value of the matrix

조회 수: 1 (최근 30일)
marwa hajji
marwa hajji 2022년 3월 17일
편집: Torsten 2022년 3월 17일
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

채택된 답변

Chunru
Chunru 2022년 3월 17일
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

추가 답변 (1개)

Torsten
Torsten 2022년 3월 17일
편집: Torsten 2022년 3월 17일
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.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by