Function to extract certain elements from a matrix

조회 수: 1 (최근 30일)
Sebastian Ciuban
Sebastian Ciuban 2014년 4월 10일
댓글: Mischa Kim 2014년 4월 10일
I have difficulties in making a function to extract columns after first number in the row from a the matrix. For example I have this matrix (A) and I want all columns that have first element 1 to be extracted in another matrix
A= 1 2 1 1 3
2 5 6 4 8
2 3 5 7 8
And I want my function to return this:
B= 1 1 1
2 6 4
2 5 7

채택된 답변

Mischa Kim
Mischa Kim 2014년 4월 10일
Ciuban, use
B = A(:,A(1,:)==1)
  댓글 수: 3
Sebastian Ciuban
Sebastian Ciuban 2014년 4월 10일
It is possible to make a function, for example if I write extract(4) and in returns me all columns that have first element 4?
Mischa Kim
Mischa Kim 2014년 4월 10일
Sure
function B = extr(A,extrnum)
B = A(:,A(1,:)==extrnum);
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by