Filter löschen
Filter löschen

how to find specific columns using vectors

1 Ansicht (letzte 30 Tage)
babis
babis am 6 Jun. 2014
Beantwortet: MiguelMauricio am 6 Jun. 2014
I have a matrix U (n*m) and vectors v(1)-v(m) [not the same size]. I want for every vector to find in the respective row (for vector v1 is row 1) the columns that have the vector number(for v1 [1; 8; 12; 52; 68; 93; 150; 189;] it selects columns 1,8,12,52,68,93,150,189) and then finds the 5 values that are closest to zero and rerurns vector v'(1)-v'(m) that have the number of the respective column(for example: v'(1)=[1;12;52;68;189;])

Antworten (1)

MiguelMauricio
MiguelMauricio am 6 Jun. 2014
I am not sure I understood it completely
newMatrix=zeros(5,m);
for i =1:m
rowU=U(:,i);
%If you meant the columns with the indexes appearing in the vector v(i)
indexes=v(i);
%If you meant the columns with the same values as in the vector v(i)
indexes=v(i)==rowU; %But v(i) and rowU must have the same length
elementsRow=rowU(indexes);
sortedElements=sort(abs(elementsRow)); %Close to zero in absolute value
newMatrix(:,i)=sortedElements(1:5);
end

Kategorien

Mehr zu Matrix Indexing 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