Problem when assigning vectors

2 visualizaciones (últimos 30 días)
Mike
Mike el 2 de Nov. de 2012
Hi,
i want to do this:
b=zeros(size(f),1)
b=f(k);
And i get this error:
Index exceeds matrix dimensions.
I print the size of each variable and this is the result:
size(f)= 500 1;
size(k)= 500 1;
size(b)= 500 1; %Here, i get this warning:
%Warning: Input arguments must be scalar.
Can anyone explain me why am i returned this result? How could i fix my mistake and make b=f(k); works??
Thank you in advance.
  3 comentarios
Mike
Mike el 2 de Nov. de 2012
Which ones?? f?? Do you want results of my signal f?? or my signal?
Azzi Abdelmalek
Azzi Abdelmalek el 2 de Nov. de 2012
f and k

Iniciar sesión para comentar.

Respuestas (3)

John Petersen
John Petersen el 2 de Nov. de 2012
the value of k must be positive and less than the size of f.
  1 comentario
Mike
Mike el 2 de Nov. de 2012
Thank you, but how could i fix the error. I do not understand why this error exists as each vector has size [ 500 1 ].

Iniciar sesión para comentar.


José-Luis
José-Luis el 2 de Nov. de 2012
b = zeros(numel(f),1);
  3 comentarios
José-Luis
José-Luis el 2 de Nov. de 2012
Editada: José-Luis el 2 de Nov. de 2012
Try
max(k)
Given your error, the value returned should be larger than the size of f. You are trying to access elements of f beyond the defined range. I recommend you read the getting started part of the documentation to understand indexing.
Mike
Mike el 2 de Nov. de 2012
Thank you for your help.

Iniciar sesión para comentar.


Loren Shure
Loren Shure el 2 de Nov. de 2012
First, Mike, there is no reason to preallocate the vector b with zeros since you overwrite the variable in the next statement.
Second, as pointed out above, the first input to zeros needs to be a scalar value.
And finally, also pointed out above, the value for k can't exceed the number of values in f.
So, I guess (and I think others above have too) that your value for k is larger than the number of elements in f. Perhaps you can post a bit more of the code so we can see better what is happening.
  1 comentario
Mike
Mike el 2 de Nov. de 2012
Thank you for your reply. I just try to preallocate the b in case i fix tmy mistake. But, i understand it is not needed.
I write:
display(max(k));
and i got the result: 4991. So, you are right.
Thank you.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by