how to find specific columns using vectors

1 次查看(过去 30 天)
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;])

回答(1 个)

MiguelMauricio
MiguelMauricio 2014-6-6
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

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by