필터 지우기
필터 지우기

how to find specific columns using vectors

조회 수: 1 (최근 30일)
babis
babis 2014년 6월 6일
답변: MiguelMauricio 2014년 6월 6일
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