Effacer les filtres
Effacer les filtres

Problem when assigning vectors

1 vue (au cours des 30 derniers jours)
Mike
Mike le 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 commentaires
Mike
Mike le 2 Nov 2012
Which ones?? f?? Do you want results of my signal f?? or my signal?
Azzi Abdelmalek
Azzi Abdelmalek le 2 Nov 2012
f and k

Connectez-vous pour commenter.

Réponses (3)

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

Connectez-vous pour commenter.


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

Connectez-vous pour commenter.


Loren Shure
Loren Shure le 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 commentaire
Mike
Mike le 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by