Largest column of a matrix

2 Ansichten (letzte 30 Tage)
Omar
Omar am 4 Jun. 2021
Kommentiert: Omar am 4 Jun. 2021
Given an m by n matrix, the e. g. A=[3, 1,2;3,1,1;3,6,2] I want write a Matlab function that finds which of the collumn of A has the highest norm and returns it. This is my attempt
function v=maxivect(A)
[m, n] =size(A);[i, j] =find(ismember(A, ceilmax((normvector(A, inf))) )); v=A(:,j);
end
Instead of getting back the vector having the maximum norm, mostly it returns empty vector while it is not. It works fine with example above but not all, for example if A=rand(3)

Akzeptierte Antwort

dpb
dpb am 4 Jun. 2021
Try
function v=maxivect(A)
[~,ix]=max(arrayfun(@(i) norm(A(:,i)),[1:size(A,2)]));
v=A(:,ix);
end
  1 Kommentar
Omar
Omar am 4 Jun. 2021
Thanks dpb, this works perfectly.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices 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!

Translated by