Non-integer value in for-loop
Afficher commentaires plus anciens
Probably a simple question but why non-integer value can't be used in for-loop.
for i=0:.1:1
H(i)=10*i ;
end
H
how do i use any non-integer value in for-loop?
Appriciate your help.
1 commentaire
Walter Roberson
le 5 Oct 2024
What does H(0.1) = value mean?
Réponse acceptée
Plus de réponses (1)
Voss
le 5 Oct 2024
0 votes
You can't use a number that's not a positive integer as an index, as in H(i) when i is 0 or 0.1, etc.. That's the problem.
vals = 0:0.1:1; % linspace(0,1,11) might be better
N = numel(vals);
H = zeros(1,N); % pre-allocate H
for i = 1:N
H(i) = 10*vals(i);
end
If that's all the loop does, you don't need it:
vals = 0:0.1:1; H = 10*vals;
Catégories
En savoir plus sur Loops and Conditional Statements 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!