Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

how can i get the minimum total from the three total since it only display the final value of the total.

조회 수: 1 (최근 30일)
hello.i want to get the minimum total from the three total.but it only display the final total(1x1)=29 not the total i expect which is total(3x1)=[26;27;29].
Below is my code.
if true
% code
end a = [4;1;9];
b = [3;1;4;6;7];
for i=1:1:3
total=0;
c=((b(i:i+2)-a).^2);
disp(c);
for i=1:1:3
total = c(i,1)+ total;
end
disp(total);
end
please help me to solve this.Thanks in advance.

답변 (1개)

Brendan Hamm
Brendan Hamm 2015년 3월 31일
편집: Brendan Hamm 2015년 4월 1일
You are changing your looping variable inside the loop. I think you want:
a = [4;1;9];
b = [3;1;4;6;7];
c = zeros(3,3); % Pre-allocate storage
for i = 1:1:3
c(:,i) = ((b(i:i+2)-a).^2);
end % End here
disp(c);
total = 0;
for i = 1:1:3
total = c(:,1) + total;
end
disp(total);
  댓글 수: 2
aziz syahirah
aziz syahirah 2015년 4월 1일
thank you for the answer but it give me an error
??? In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ==> m1 at 32 c(i) = ((b(i:i+2)-a).^2);

태그

Community Treasure Hunt

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

Start Hunting!

Translated by