error with results, matlab
2 views (last 30 days)
Show older comments
Where is error? My program showed my only WYKRYTO, but I don't know what.
fprintf('\nWYKRYTO: ');
if (kaszel == max([kaszel, smiech, krzyk, chrzakanie]))
fprintf('kaszel');
elseif (smiech == max([kaszel, smiech, krzyk, chrzakanie]))
fprintf('smiech');
elseif (krzyk == max([kaszel, smiech, krzyk, chrzakanie]))
fprintf('krzyk');
elseif (chrzakanie == max([kaszel, smiech, krzyk, chrzakanie]))
fprintf('chrzakanie');
end
13 Comments
Accepted Answer
Walter Roberson
on 21 Jan 2014
max([array1, array2])
means
max(horzcat(array1, array2))
which in turn means
max(horzcat(array1, array2), [], 1)
which will calculate the column-by-column maximum of horzcat(array1, array2), which is going to give you a result which is 1 x (size(array1,2) + size(array2,2)) .
And then you try to compare the entire array array1 to the result of the max(), so you are comparing
size(array1,1) x size(array1,2)
to
1 x (size(array1,2) + size(array2,2))
and you can see by examination that the only way that can work is if array1 only has a single row and array2 is empty. (Well, that case would be treated differently by MATLAB, but you would still end up with mismatched lengths for the comparison)
Taking into account that you have entire arrays to be compared, what do you want your output to be? A 2D cell array of strings that indicates at each location which of the four arrays was the maximum at that point ? Or do you wish to know which of the four has the greatest average magnitude? Or something else?
9 Comments
More Answers (2)
per isakson
on 21 Jan 2014
2 Comments
Matt J
on 21 Jan 2014
Hard to imagine that applies to max() operations. I'm not encountering issues, at least.
per isakson
on 22 Jan 2014
Edited: per isakson
on 22 Jan 2014
Agree, but it's a good habit not to use "==" with floats.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!