Matlab for loop trouble -
i'm trying understand loops better in matlab since reason don't work other languages.
values = zeros(1,7); =[1, 4, 10, 15, 22, 24, 36] variable = function(input, input, i); values(i) = function(input, variable); end
so values becomes 1x36 double i results, number isn't gets column set 0. why still add columns values didn't choose i?
thanks
you preallocating values
7
entries. when assign value @ entry 10
, entries 8
, 9
must created too, , matlab fills them 0
.
here's example:
>> clear >> values = ones(1,7) % preallocate ones values = 1 1 1 1 1 1 1 >> values(4) = 40 values = 1 1 1 40 1 1 1 >> values(10) = 100 % array have grow values = 1 1 1 40 1 1 1 0 0 100
note having array growing not desirable, negatively impacts speed. it's better preallocate final size, whenever known in advance.
thanks @steve edit
if want results stored in consecutive entries, consider using counting variable, e.g. k
indexing increases 1
each step of loop.
values = zeros(1,7); % k stores number of steps of loop k = 0; =[1, 4, 10, 15, 22, 24, 36] % increment k k = k+1; variable = function(input, input, i); values(k) = function(input, variable); end
Comments
Post a Comment