Pergunta

I have defined a matrix (initial_matrix) equal to 0 by using a for loop as:

I = 5;   % e.g number of nodes
for i =1:I
    initial_matrix = [0];    // an initial matrix will be generated for each node
end

Now, for each node i, I will consider all other nodes but not the node i itself and subtract each of them from 1 and take their product:

for node 1:

result = (1 - initial_matrix of node 2) * (1 - initial_matrix of node 3) * ...
    (1 - initial_matrix of node 4) * (1 - initial_matrix of node 5)

for node 2:

result = (1 - initial_matrix of node 1) * (1 - initial_matrix of node 3) * ...
    (1 - initial_matrix of node 4) * (1 - initial_matrix of node 5)

for node 3:

result = (1 - initial_matrix of node 1) * (1 - initial_matrix of node 2) * ...
    (1 - initial_matrix of node 4) * (1 - initial_matrix of node 5) 

and so..for the remaining 2 nodes!

Can any one tell me or give me hints on how this can be achieved? Thanks!

Foi útil?

Solução

For each product (per node) you need to have all initial matrices in advance, so you should modify your initial loop to something along these lines:

initial_matrix = cell(I, 1);
for i = 1:I
    initial_matrix{i} = blah blah... %// Generated for each node
end

Then you can add another nested loop that does something like the following:

result = cell(I, 1);
for k = 1:I

    %// Compute product
    result{k} = 1;
    for n = setdiff(1:I, k)
        result{k} = result{k} * (1 - initial_matrix{n});
    end
end

Outras dicas

I think these are the steps you need to take:

Assuming all matrices are of the same size and that you want to do elementwise multiplication:

  1. Concatenate all of them into one big matrix initialMatrices, as desribed here.
  2. Apply cumprod on 1-initialMatrices
  3. If you used cumprod Extract the matrices that you need from this result

Some things to pay attention to:

  • Maybe preprocess the first initial matrix by doing one minus it, or remove it.
  • Make sure you build the total matrix in the right 'direction'. Check help cumprod for what the right direction is.

Currently, you are not creating an initial matrix for each node. If you want to have a separate one for each, I would suggest you to define a structure. So you will have:

I = 5;
for i =1:I
    initial_matrix.(sprintf('Node%d',i)) = [0];    % an initial matrix will be generated for each node
end

Then you can maybe do something like the following to do your operation (if I understood it right):

    for i =1:I
        numnode=[1:I]
        numnode(numnode==i)=[]
        for ind = 1:numel(numnode)-1
            NewMatrix.(sprintf('Node%d',i)) = (1- initial_matrix.sprintf('Node%d',numnode(ind))))*(1- initial_matrix.(sprintf('Node%d',numnode(ind+1))))
            end
    end

You can verify if this is correct by using a rand(1) statement instead of [0] while defining the initial_matrix.

I hope I understood you correctly

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top