How do I call specific rows of a matrix?

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".

 Accepted Answer

Image Analyst
Image Analyst on 15 May 2015
Edited: Image Analyst on 15 May 2015
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 Comments

Sorry that was unclear, I have a function that outputs "rows", but when I try to call A(rows,:) it returns the error, A is always a 2D array.
Does my code work for you? If so, what is different about your code. When you type rows all by itself on the command line and whos, what does it say:
rows
whos rows
whos A
It says that rows is size 18x1, which I would expect, class double. Could it be the class that's doing it? I'm really confused because when I ask for A(rows,:) by itself after I run the function it will give it to me and it's what I would expect, but during the function it gives me the error. So it's working and it isn't haha. I think it might be my computer. Strange stuff like this has happened before and then when I run the code on a friends computer it works.
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?
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!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!