#Day4- #100DaysChallenge- Matlab Loops| Find Most Non zeros Row in a matrix
#Day4-Find Most Non-Zeros Row in a matrix
Note: This code can be done using the in-built command. But for the challenge I am trying to avoid those.
a=[1 2 4 5 0
0 2 3 0 0
3 4 0 0 0
0 0 0 0 1]
If the above matrix is given then the code has to return as 1 which means Row 1 has most non zero entries.
Matlab code:
Join us on Telegram: https://t.me/matlabirawen
Join us on Facebook Group: https://www.facebook.com/groups/matlabcodes
Task: Find Most Non-Zero Rows in a given Matrix
Note: This code can be done using the in-built command. But for the challenge I am trying to avoid those.
a=[1 2 4 5 0
0 2 3 0 0
3 4 0 0 0
0 0 0 0 1]
If the above matrix is given then the code has to return as 1 which means Row 1 has most non zero entries.
Matlab code:
function r =
fullest_row(a)
[m,n]=size(a);
for i=1:1:m
vec(i)=0;
max_value=0;
for j=1:1:n
if (a(i,j)~=0)==1
vec(i)=vec(i)+1;
end
end
end
for ii=1:1:m
if max_value<vec(ii)
max_value=vec(ii);
r=ii;
end
end
end
Sample Input and Output
a =
3 0 0 0
3 3 3 0
5 0 0 2
1 0 0 0
r = fullest_row(a)
r =
2
Video Description
Join us on Facebook Group: https://www.facebook.com/groups/matlabcodes
No comments