find the minimum value in each row without using min
1 view (last 30 days)
Show older comments
I can't use function min And here is my code
x = rand([4,3])
for i = 4;
row = 0;
for j = 1:3
if x(i,j) >=0
row = row + x(i,j);
end
end
fprintf('The row %d is %d\n',i,row)
end
The answer displayed 'The row 4 is 2.245814e+00' What's wrong with my code? Can anyone help me out? thanks
0 Comments
Accepted Answer
Image Analyst
on 3 Nov 2014
Wrong in lots of ways. Rather than explain them all, just study my corrections:
x = rand([4,3])
[rows, columns] = size(x);
for row = 1 : rows;
theRowMins(row) = inf; % Set to infinity
for col = 1: columns
if x(row,col) < theRowMins(row)
theRowMins(row) = x(row,col);
end
end
fprintf('The min of row %d is %f\n',row,theRowMins(row))
end
% Print to command window:
theRowMins
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!