Problem when assigning vectors
Mostrar comentarios más antiguos
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
Azzi Abdelmalek
el 2 de Nov. de 2012
can you post a sample of your data
Mike
el 2 de Nov. de 2012
Azzi Abdelmalek
el 2 de Nov. de 2012
f and k
Respuestas (3)
John Petersen
el 2 de Nov. de 2012
1 voto
the value of k must be positive and less than the size of f.
1 comentario
Mike
el 2 de Nov. de 2012
José-Luis
el 2 de Nov. de 2012
b = zeros(numel(f),1);
3 comentarios
Mike
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
el 2 de Nov. de 2012
Loren Shure
el 2 de Nov. de 2012
1 voto
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
el 2 de Nov. de 2012
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!