フィルターのクリア

Printing Results out of a for Loop

4 ビュー (過去 30 日間)
Ammar Taha
Ammar Taha 2019 年 6 月 27 日
コメント済み: Ruger28 2019 年 6 月 27 日
I have Written a Small Function that Returns the Absolute Difference Between the Max and Min Elements of Each Row in a Matrix and Assigns the Results to the output "rows_diff" as Row Vector and Also Gets the Diff. Between the Max and Min of Whole Matrix in "tot_diff"
All what I want to Know is How to Print Out the Results of the for Loop Returning them in "rows_diff" as row vector not just the Last Result as the Case with this Version of the Function:-
function [rows_diff, tot_diff] = minimax(M)
a = size(M,1);
for i = 1:a
rows_diff = abs(max(M(i,:)) - min(M(i,:)));
end
tot_diff = max(M(:)) - min(M(:));
  2 件のコメント
Stephen23
Stephen23 2019 年 6 月 27 日
編集済み: Stephen23 2019 年 6 月 27 日
Note that avoiding the loop gives simpler, more efficient code:
>> M = magic(5) % fake data.
M =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>> minimax(M) % function with loop (following Ruger28's bugfix).
ans =
23 18 18 18 23
>> max(M,[],2) - min(M,[],2) % no loop required, more efficient.
ans =
23
18
18
18
23
Ammar Taha
Ammar Taha 2019 年 6 月 27 日
Thanks Again Stephen, but I was doing an Assigment for a Course and it was Required to use for Loop Specificaly.
I have read this before and I will try to Aviod Looping in Future.

サインインしてコメントする。

採用された回答

Ruger28
Ruger28 2019 年 6 月 27 日
rows_diff = abs(max(M(i,:)) - min(M(i,:)));
This overwrites the value of rows_diff each time. This is ok if you just want to print it out, but to store it (which seems like you want to) you need:
rows_diff(i) = abs(max(M(i,:)) - min(M(i,:)));
to print this value, in your loop place a fprintf
fprintf('Value of rows_diff = %f\n',rows_diff(i))
  2 件のコメント
Stephen23
Stephen23 2019 年 6 月 27 日
編集済み: Stephen23 2019 年 6 月 27 日
Remember to preallocate any arrays before the loop:
Ammar Taha
Ammar Taha 2019 年 6 月 27 日
Thanks alot Stephen Matlab has Pointed this Out and I Made it :)

サインインしてコメントする。

その他の回答 (2 件)

Alex Mcaulley
Alex Mcaulley 2019 年 6 月 27 日
You can also implement the function in a simpler and efficient way:
function [rows_diff, tot_diff] = minimax(M)
rows_diff = arrayfun(@(i) abs(max(M(i,:)) - min(M(i,:))),1:size(M,1))';
tot_diff = max(M(:)) - min(M(:));
end
  3 件のコメント
Ammar Taha
Ammar Taha 2019 年 6 月 27 日
Thanks Alex Mcaulley this is efficient and neat :)
Ammar Taha
Ammar Taha 2019 年 6 月 27 日
I have got Benefits from your solution Stephen Cobeldick it Forced me to Review the Format of (max), but as I said it was required to Use Loops
But at all Thank you for your Attention :)

サインインしてコメントする。


Ammar Taha
Ammar Taha 2019 年 6 月 27 日
Thank you Ruger28 it Worked :)
  1 件のコメント
Ruger28
Ruger28 2019 年 6 月 27 日
no problem, glad to be of some help.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by