Error: Array indices must be positive integers or logical values.

1 vue (au cours des 30 derniers jours)
Carlos Dolz
Carlos Dolz le 29 Oct 2020
Commenté : David Hill le 29 Oct 2020
I have the following code:
theta = [0,45,60]
for i =1:3
if i-1 == 0;
theta(i-1) = 0
end
end
x=cosd(theta(i-1))
end
I keep getting the same error, I think its because you can't have an array of elements with an element at position zero, but I have already specified what I want it to be when that happens. Howc an I fix this?

Réponses (2)

Ameer Hamza
Ameer Hamza le 29 Oct 2020
Modifié(e) : Ameer Hamza le 29 Oct 2020
I am not sure what you are trying to do in your code. The following should work just fine
theta = [0,45,60]
x = cosd(theta)

David Hill
David Hill le 29 Oct 2020
You are still indexing at zero. If you just want cosd at each theta then:
x=cosd(theta);
  2 commentaires
Carlos Dolz
Carlos Dolz le 29 Oct 2020
My issue is that I want to have an index at zero, and knowing that in theory this is not possible, can't one specify a value for in this case theta when the index is zero?
I was trying to do that with the if statement
David Hill
David Hill le 29 Oct 2020
Matlab does not allow indexing at zero. Indexing starts at 1. You never need to index at zero (you can always get around it).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by