how can i get the minimum total from the three total since it only display the final value of the total.
情報
この質問は閉じられています。 編集または回答するには再度開いてください。
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
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
2015 年 4 月 1 日
Brendan Hamm
2015 年 4 月 1 日
Edited the answer. I apologize, I didn't have access to MATLAB before.
この質問は閉じられています。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!