function indices = saddle(M) [ rows,cols ] = size(M); [valR,posR] = max(M,[],2); [valC,posC] = min(M,[],1); indices= []; for i = 1:length(posR) if i == posC(posR(i)) indices= [indices; i, posR(i)]; end end end
It is running fine. But when i test my code for
>> mat=zeros(5,3) mat = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I am getting wrong output : saddle(mat) ans = 1 1
the correct output must be
saddle(mat) ans = 1 1 2 1 3 1 4 1 5 1 1 2 2 2 3 2 4 2 5 2 1 3 2 3 3 3 4 3 5 3
What i am doing wrong??
Copying code from the internet is not always a good way to learn best-practice coding: Solving this problem using loops misses using MATLAB's excellent code vectorization abilities. It would be much neater and faster to use bsxfun instead, like this:
function idx = saddle(mat) row_mx = bsxfun(@ge,mat,max(mat,[],2)); col_mn = bsxfun(@le,mat,min(mat,[],1)); [R,C] = find(row_mx & col_mn); idx = [R,C]; end
Which gives this:
>> saddle(zeros(5,3)) ans = 1 1 2 1 3 1 4 1 5 1 1 2 2 2 3 2 4 2 5 2 1 3 2 3 3 3 4 3 5 3
Matlabsolutions.com provides guaranteed satisfaction with a
commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain
experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support
to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been
empanelled after extensive research and quality check.
Matlabsolutions.com provides undivided attention to each Matlab
assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services
include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work
done at the best price in industry.