-2

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!

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
Mokujin
  • 65
  • 6
  • This is very difficult to follow. Can you provide an example? – Dan Sep 11 '13 at 08:58
  • @Dan. The above example was for the case when node 1 is considered. Now if I consider node 2: then i should get: 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) ....similarly for node 3 etc – Mokujin Sep 11 '13 at 09:21
  • This is not an example. If the answers below aren't what you're looking for, you should ADD TO YOUR QUESTION (i.e. not a comment) a numerical small worked example – Dan Sep 11 '13 at 09:30
  • Do you really want the matrix product, or the elementwise product? – Dennis Jaheruddin Sep 11 '13 at 09:42

3 Answers3

1

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
Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • @ Eitan T, but this wont run for node 2 and so on..coz for node 1 i will consider 2,3,4 and 5, then for node 2: 1,3,4 and 5, for node 3: 1,2,4 and 5....and so on.... – Mokujin Sep 11 '13 at 09:09
  • So for each node you want to consider all nodes but itself? Please take the time to add this missing information to the question. – Eitan T Sep 11 '13 at 09:13
  • yes, i want to consider all the nodes expect the node in question! – Mokujin Sep 11 '13 at 09:16
  • Especially when you set it to zero, you will want to do: `prod(1-initial, 3)` -- This applied to your previous version, I can look at the udated one later. – Dennis Jaheruddin Sep 11 '13 at 09:33
  • @DennisJaheruddin You can't use `prod` and `cumprod` because they do element-wise multiplication, not matrix multiplication... – Eitan T Sep 11 '13 at 09:37
  • 1
    @EitanT: Ok. I am checking the code – Mokujin Sep 11 '13 at 09:42
  • @user2762192 Please use the update code that employs cell arrays :) – Eitan T Sep 11 '13 at 09:49
  • @EitanT, i get the following error: Cell contents reference from a non-cell array object. Error in colll (line 14) result{k} = eye(size(result{k})); – Mokujin Sep 11 '13 at 10:04
  • @user2762192 You're right, forgot to updated that. It's fixed now. – Eitan T Sep 11 '13 at 10:07
0

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.
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
0

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

Yunus
  • 63
  • 4
  • But where's the part that does multiplication? – Eitan T Sep 11 '13 at 09:41
  • @Eitan just realized my mistake and modified it. Should be multiplying now. – Yunus Sep 11 '13 at 09:43
  • One more thing: while it certainly works, it seems to me that using structs and dynamic referencing is an unnecessary over-complication. Using cell arrays here definitely would be much cleaner. – Eitan T Sep 11 '13 at 09:51