Creating vector of labels

4 vues (au cours des 30 derniers jours)
Monalisa Chikezie
Monalisa Chikezie le 5 Août 2022
I want to assign pics_ds the value 1 and words_ds -1. Both pics_ds and words_ds are 198x1 double. How do I do this? So far, I have tried:
pics_ds = 1; % assigning the label '1' to pictures
words_ds = -1; % assigning the label '-1' to words
this changes the entire data in pics_ds and words_ds to 1 and -1 (Just one number)
I also tried this
pics_ds = [zeros(length(pics_ds),1)];
words_ds = [ones(length(words_ds),-1)];
This assigns pics_ds a vector of zeros i.e 198x1 0 (Would have preferred it as 1's). Then it assigns an empty squared bracket to words_ds.

Réponses (1)

dpb
dpb le 5 Août 2022
pics_ds = ones(size(pics_ds));
words_ds = -ones(size(words_ds));
MATLAB reallocates the LHS variable when not subscripted to the RHS on assignment -- hence using just the constant "1" does, indeed, leave you with an array of only one element as you discovered.
The second try is almost right but you seemed to have confused the second argument with the value you were trying to assign; both arguments in zeros and ones are sizes -- to set the first two dimensions of the result. By default, if you supply only one value, then they return a square array of that size so to create a vector, one or the other of the two arguments must be "1" -- which will determine if the result is a row or column vector.
length() returns only one value and array indexing/sizes must begin at 1 so the "-1" in the second is an illegal size argument; hence the empty result.
To create a new vector the size of an existing one, use size on the existing as above...
  1 commentaire
Monalisa Chikezie
Monalisa Chikezie le 6 Août 2022
Alright, thank you for this explanation

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by