Вопрос

Given weight vector

w = [0.1, 0.1, 0.1, 0.1, 0.1];

Should result in

wg = [0.15 0.05 0.1  0.1  0.1; 
      0.15 0.1  0.05 0.1  0.1; 
      0.15 0.1  0.1  0.05 0.1; 
      0.15 0.1  0.1  0.1  0.05; 
      0.05 0.15 0.1  0.1  0.1; 
      0.1  0.15 0.5  0.1  0.1; ...] 

so there will be the matrix with possible allocations so each element of w will increase and decrease to 0.05.

I tried using loop, but it seems to be incorrect approach, because there is a situation when index exceeds matrix dimensions. Is it possible to use loop? Any solution? Thank you.

Это было полезно?

Решение

It is possible with a loop:

w = [0.1, 0.1, 0.1, 0.1, 0.1];
n = length(w);

adj = [0.05*ones(n-1,1), -0.05*eye(length(w)-1)];

wg = bsxfun(@plus, w, adj);
for c = 2:n
   adj(:,[c-1, c]) = adj(:,[c, c-1]);
   wg = [wg; bsxfun(@plus, w, adj)];
end

which results in

wg =

   0.150000   0.050000   0.100000   0.100000   0.100000
   0.150000   0.100000   0.050000   0.100000   0.100000
   0.150000   0.100000   0.100000   0.050000   0.100000
   0.150000   0.100000   0.100000   0.100000   0.050000
   0.050000   0.150000   0.100000   0.100000   0.100000
   0.100000   0.150000   0.050000   0.100000   0.100000
   0.100000   0.150000   0.100000   0.050000   0.100000
   0.100000   0.150000   0.100000   0.100000   0.050000
   0.100000   0.050000   0.150000   0.100000   0.100000
   0.050000   0.100000   0.150000   0.100000   0.100000
   0.100000   0.100000   0.150000   0.050000   0.100000
   0.100000   0.100000   0.150000   0.100000   0.050000
   0.100000   0.050000   0.100000   0.150000   0.100000
   0.100000   0.100000   0.050000   0.150000   0.100000
   0.050000   0.100000   0.100000   0.150000   0.100000
   0.100000   0.100000   0.100000   0.150000   0.050000
   0.100000   0.050000   0.100000   0.100000   0.150000
   0.100000   0.100000   0.050000   0.100000   0.150000
   0.100000   0.100000   0.100000   0.050000   0.150000
   0.050000   0.100000   0.100000   0.100000   0.150000

Which is what I think you're after. It's probably possible without a loop as well but I'm sure this will suffice.

Другие советы

You want to add/subtract each weight either -.05, 0 or .05 weight. To cover all possibilities (3 for 5 elements in 5, total 3^5 options):

>> n = length( w );
>> [x{1:n}] = ndgrid( -1:1 );
>> x = cellfun( @(y) y(:), x, 'uni', 0 );
>> wg = bsxfun(@plus, w, [x{:}]*.05 );
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top