Problem with array filling
Afficher commentaires plus anciens
Hi,
I need to do this:
for i=0.1:0.1:360
j(i*10)=2;
end
but I obtain this error message:
??? Attempted to access j(3); index must be a positive integer or
logical.
Could you help me?
Thanks in advance
Thomas
Réponse acceptée
Plus de réponses (4)
Oleg Komarov
le 17 Août 2012
Modifié(e) : Oleg Komarov
le 17 Août 2012
sprintf('%.17g', 0.3)
ans =
0.29999999999999999
The reason you get this error is because of floating point arithmetic.
Look closely:
isequal((0.1:0.1:1)*10,1:10) % NO!
Here is the closer look:
x = (0.1:0.1:1)*10;
x==(1:10) % Notice the third return is a zero, here's why:
fprintf('%.16f\n%.16f\n',x(3),3) % Print out and compare...
Azzi Abdelmalek
le 17 Août 2012
Modifié(e) : Azzi Abdelmalek
le 17 Août 2012
in matlab index must be a real strict positiv integer
it does'nt accept x(0) or x(-1)
h=zeros(360*10/0.1,1)
for j=0.1:0.1:360
ind=int16(j*10) % convert to integer, for biger number, u can use int32, int64
h(ind)=2;
end
thomas82
le 18 Août 2012
0 votes
Catégories
En savoir plus sur Creating and Concatenating Matrices 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!