MATLAB: Subscript indices must either be real positive integers or logicals.

MATLABsubscript indices

dear all
i have code like below
clc
clear all
for b=1:0.1:20
M((b*10)-9,:)=[1 1 1 ]
end
but at b=2.4 i got this error :
M =
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
Subscript indices must either be real positive integers
or logicals.
Error in Untitled2 (line 4)
M((b*10)-9,:)=[1 1 1 ]
i really appreciated if some can help me y i get this error and what the solotions ?

Best Answer

Better to iterate over integer indices rather than fractional values:
V = 1:0.1:20;
N = numel(V);
M = nan(N,3);
for k = 1:N
M(k,:) = [1,1,1];
end
Even better:
M = repmat([1,1,1],N,1)
Best:
M = ones(N,3)