how to find specific columns using vectors

1 view (last 30 days)
babis
babis on 6 Jun 2014
Answered: MiguelMauricio on 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;])

Answers (1)

MiguelMauricio
MiguelMauricio on 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

Categories

Find more on Optimization in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!