To find the value in a vector for the corresponding indices from another vector

8 次查看(过去 30 天)
Hi Everyone,
I have small issue in finding the values from a vector for the corresponding indices from another vector. for ex:
If I have a vector:
aa = [0.023 0.987 0.112 0.666 0.2734, .... , 0.6667]; % say dimension of 1x1000
and
bb = [34 12 67 87 76 83 11 97 23 112 298]; % say dimension of 1x50 (which are the index values for which I want the corrsponding values from aa.
I don't want to use a for loop!!
I did something wrong like
cc = aa(bb(1:end));
error message: Index exceeds matrix dimensions. %(because it does not make sense)
Can anyone suggest me any solution! It would of great help.
Thanks in advance!
Cheers, Savan

回答(2 个)

the cyclist
the cyclist 2017-6-20
编辑:the cyclist 2017-6-20
Your largest value of bb must be longer than the length of aa.
You can check this by comparing
max(bb)
with
length(aa)

Gopichandh Danala
Gopichandh Danala 2017-6-20
There is nothing wrong in your code, maybe some of the indices in bb are higher than length of aa, which is giving that error.
Just verify
logical(max(bb) < length(aa))
if its true (1), there is no problem , if its false (0) some index in bb is higher than length of aa and causing the error

类别

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