Index exceeds the number of array elements (3).
Afficher commentaires plus anciens
n = input('What"s your ""favorite"" number? ')
twiceYourFavoritePlusOne = 2*n + 1
myFavorites = linspace(2,8,3)
yourFavorites = 2:3:6
myFavorites(yourFavorites)
yourFavorites(myFavorites) = 6
myFavoritesSquares = myFavorites.*myFavorites
if (rem(n,2) == 0)
disp('even')
else
disp('odd')
end
ages4review = [19 7 4 2; 14 89 62 50; 2 3 2 12]
Réponses (1)
What is the purpose of your code?
n = 4; %input('What"s your ""favorite"" number? ')
twiceYourFavoritePlusOne = 2*n + 1
myFavorites = linspace(2,8,3)
yourFavorites = 2:3:6
myFavorites(yourFavorites)
% myFavorites has only 3 numbers. yourFavourites has index 2 and 5.
% The index 5 exceeds the number of elements in myFavorites.
yourFavorites(myFavorites) = 6
myFavoritesSquares = myFavorites.*myFavorites
if (rem(n,2) == 0)
disp('even')
else
disp('odd')
end
ages4review = [19 7 4 2; 14 89 62 50; 2 3 2 12]
5 commentaires
Pimpawee Khumsup
le 4 Sep 2021
Chunru
le 4 Sep 2021
You need to elaborate what you want to do with the code. Correct the grammar is not meaningful.
Walter Roberson
le 4 Sep 2021
myFavorites(yourFavorites)
Maybe you wanted to use
intersect(myFavorites, yourFavorites)
Pimpawee Khumsup
le 4 Sep 2021
Walter Roberson
le 4 Sep 2021
The only dots I see in the code are in
myFavoritesSquares = myFavorites.*myFavorites
but that is not invalid.
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!