Problem when assigning vectors

5 views (last 30 days)
Mike
Mike on 2 Nov 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 Comments
Mike
Mike on 2 Nov 2012
Which ones?? f?? Do you want results of my signal f?? or my signal?

Sign in to comment.

Answers (3)

John Petersen
John Petersen on 2 Nov 2012
the value of k must be positive and less than the size of f.
  1 Comment
Mike
Mike on 2 Nov 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 ].

Sign in to comment.


José-Luis
José-Luis on 2 Nov 2012
b = zeros(numel(f),1);
  3 Comments
José-Luis
José-Luis on 2 Nov 2012
Edited: José-Luis on 2 Nov 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 on 2 Nov 2012
Thank you for your help.

Sign in to comment.


Loren Shure
Loren Shure on 2 Nov 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 Comment
Mike
Mike on 2 Nov 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!