How do I get the value at a particular index?

So I have indices corresponding to values that I want. Is there a MATLAB function that would allow me to output the values when I input the indices of those values?

5 Comments

I don't understand. If k is the index, then A(k) is the value of A at index k. What else are you looking for?
If you have set of indicies in a vector, you can do A(vec) to return all the A values at that set of indicies rather than looking at each index individually:
X=2:2:10; %[2 4 6 8 10]
ind=[1,2,4]; %vector of indicies to output values for
A=X(ind); %A=[2 4 8]
There is a slightly different technique for the case where you have a list of row indices and a list of column indices, and you want to extract at corresponding pairs.
Yes the techniques there are good, except I would not use the diag() one

Sign in to comment.

Answers (1)

Hi
Consider A as the matrix, and x as indices, then
Values = A(x)
In case you need value of a 2-D matrix, define 'rowMatrix' which has the row indices and 'colMatrix' which has corresponding column indices, then
Values = A(rowMatrix, colMatrix)

Asked:

on 24 Oct 2019

Commented:

on 1 Mar 2020

Community Treasure Hunt

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

Start Hunting!