Jhon Macieira
Jhon Macieira

Reputation: 9

Matlab, I need to find the row with the smallest sum of its elements

  E = M;
fbest = inf;
for k = 1:Rows
    if sumsqr(E(k,1:Columns)) < fbest
        fbest = sumsqr(E(k, 1:Columns));
        xbest = E(k, 1:Columns);
    end
end

E is the matrix i need to find which row has the smallest square root of its added values, I the output gives me fbest= inf and nothing for xbest. I cant seem to see why it isnt working.

Note

I'm working in Matlab 2019a

Upvotes: 0

Views: 64

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 102710

Maybe you can try

[val,idx] = min(sqrt(sum(E.^2,2)))

Upvotes: 0

rinkert
rinkert

Reputation: 6863

You can use vectorization and the min function.

E_sumsqr = sqrt(sum(E.^2, 2)) ; % determine square root of sum of squares per row
[min_value, min_index] = min(E_sumsqr) % get the minimum value and index of the row
E_minrow = E(min_index, :) 

Upvotes: 1

Related Questions