For loop iteration issue
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a binary thresholded Image that I want to loop through its matrix using a for loop.
In python, I would write the code like this:
for i in binaryImage:
...
(you get the point!)
How do I loop through the binaryImage matrix I have in MATLAB for further operations?
0 Kommentare
Antworten (1)
Steven Lord
am 20 Mär. 2021
Bearbeitet: Steven Lord
am 20 Mär. 2021
Loop through elements?
A = magic(4);
s = 0;
for elt = 1:numel(A)
s = s + A(elt);
end
fprintf("The sum of elements in a 4-by-4 magic sum is %d.", s)
Through columns?
cs = zeros(4, 1);
for col = 1:width(A)
cs = cs + A(:, col);
end
fprintf("The sum of the columns is")
disp(cs)
or you can just use the array as the indices.
cs2 = zeros(4, 1);
for col = A
cs2 = cs2 + col;
end
fprintf("The sum of the columns is also")
disp(cs2)
Through rows?
rs = zeros(1, 4);
for row = 1:height(A)
rs = rs + A(row, :);
end
fprintf("The sum of the rows is")
disp(rs)
Do you need to loop?
Depending on what you want to do, though, you may not need to loop.
fprintf("A has %d elements greater than 11", nnz(A > 11))
Siehe auch
Kategorien
Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!