How do I call specific rows of a matrix?

조회 수: 23 (최근 30일)
Isaac Knee
Isaac Knee 2015년 5월 15일
댓글: Isaac Knee 2015년 5월 16일
I have a function who's output is a 1 dimensional array of random numbers i.e. rows = [1 2 5 19], how do I call those corresponding rows from a matrix i.e. A(1 2 5 19,:)? I understand how to do it step by step in the command window but I want a parent function to be able to call them no matter what they are or how many elements are in 'row'. I tried A(rows,:) but it returned "index exceeds matrix dimensions".

채택된 답변

Image Analyst
Image Analyst 2015년 5월 15일
편집: Image Analyst 2015년 5월 15일
Not sure what you tried, but this works perfectly fine:
rows = [1 2 5 19]
A = randi(9, 25, 2) % Sample data.
output = A(rows,:) % Extract rows 1, 2, 5, and 19 only.
Of course when you say "A(1 2 5 19,:)" that implies that A is a 2D array, but you said you want "a function who's output is a 1 dimensional array of random numbers" so I'm not exactly sure what 1D column vector you want. What would you have for your 1D output?
  댓글 수: 5
Walter Roberson
Walter Roberson 2015년 5월 15일
what is size(A) and what is max(rows) ?
Is it possible that it should be a column index instead of a row index?
How was the index determined?
Isaac Knee
Isaac Knee 2015년 5월 16일
I figured out the problem eventually, the rows I had calculated before had been indexed wrong and that's what caused the problem. Thanks for the help!

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

추가 답변 (0개)

카테고리

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