Wrong way to index: Index exceeds matrix dimensions

1 Ansicht (letzte 30 Tage)
Fernando
Fernando am 20 Mär. 2013
Bearbeitet: Hideto Koizumi am 28 Aug. 2018
Hi,
I'm trying to use indices to expand a vector but I keep getting an error message and I don't understand why. In particular, what I'm trying to do works in an example such as
x=[1 2 3]';
y=[.1 .5 .3]';
z=[1 1 2 2 3];
yz=y(z);
Here I have two vectors, x and z that have the same values for the indices. I want to expand the vector y to have the same dimensions as z but the values of z must be repeated according to how the index is repeated.
Now, in my application this is not working. I have two vectors X and Z, X of dimensions 2270x1 and Z of dimensions 446723x1 that contains the same indices as X but a lot of times (and each number is repeated a different number of times). To check that the indices are the same I used isequal(unique(X),unique(Z)) and the output was 1.
Finally, I have a vector Y of dimensions 2270x1 that plays the role of y in the example above. When doing YZ=Y(Z) I get ??? Index exceeds matrix dimensions.
Any ideas what is going on?
Thanks,

Akzeptierte Antwort

ChristianW
ChristianW am 20 Mär. 2013
max(Z) is greater then 2270?
  3 Kommentare
Fernando
Fernando am 21 Mär. 2013
This works, thanks!
Hideto Koizumi
Hideto Koizumi am 28 Aug. 2018
Bearbeitet: Hideto Koizumi am 28 Aug. 2018
I think this method could cut off the largest element of z, if z is something like z = [1 1 2 4] and y=[.1 .5 .3 .7]';

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Azzi Abdelmalek
Azzi Abdelmalek am 20 Mär. 2013
Bearbeitet: Azzi Abdelmalek am 20 Mär. 2013
Check this
size(Y)
max(Z)
What did you get?

Fernando
Fernando am 20 Mär. 2013
Thanks. Yes, the max of Z is greater than the number of elements in Y, which generates the message. Is there any way to overcome this? Following on the example, if this was
x=[1 2 5]';
y=[.1 .5 .3]';
z=[1 1 2 2 5];
yz=y(z);
I get the message because max(z)> size(y). How can I get yz to be yz=[.1 .1 .5 .5 .3]';?
Thanks,

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by