-1

This is probably an extremely remedial question, so I apologize in advance, however I'm pretty new to MATLAB and keep getting stumped at this simple problem.

So, I have an arbitrary matrix (D) that denotes a directed network:

D = [0,1,1,0,0,0,0;
     0,0,0,1,1,0,0;
     0,0,0,0,1,0,0;
     0,0,0,0,0,1,0;
     0,0,0,0,0,1,0;
     0,0,0,0,0,0,1;
     0,0,0,0,0,0,0]

n = length(D);

All that I want to do is count the out-degree of each node. This can be calculated easily using the command:

O = cumsum(D,2);
O1 = (1,n);
... for all n in D...

I am just trying to write a loop command so that the script counts the out-degree of each node in the network and when doing so creates a new variable. I wrote the following loop command:

O = cumsum(D,2);
for i=1:n
    O_i = O(i,n)
end

However, I keep just updating the same variable 'O_i' as opposed to creating new variables, 'O_1,...,O_7' :( .

Is there any way to create a new variable for each loop??

Many thanks,

Owen

sebastian
  • 9,526
  • 26
  • 54
Owen
  • 13
  • 6
  • 1
    Why don't you store the results in an array? – darmat Sep 24 '13 at 18:59
  • 1
    duplicate:http://stackoverflow.com/questions/2809635/how-to-concatenate-a-number-to-a-variable-name-in-matlab – Bas Swinckels Sep 24 '13 at 19:08
  • 3
    This question gets asked a lot, and has nothing to do with adjacency matrices or graph-theory. See the duplicate question above, it is usually considered bad practice to define variables `a_i` in a loop, it is better to put everything in a vector and do simple indexing: `a(i)`. – Bas Swinckels Sep 24 '13 at 19:10
  • Apologies for the incompetence there; thanks for your help – Owen Sep 26 '13 at 15:08

2 Answers2

0

What you want is essentially an array, thankfully, matlab is quite good with that, you can simply use O(i), but it's better to initialize first: O=zeros(size(D,2),1).

that been said, in this case all you really need is the sum function: O=sum(D,2), here O(i) will be the out-degree of node i

pseudoDust
  • 1,336
  • 1
  • 10
  • 18
-2

Assuming that you want matlab to take "O_i" as a variable, you want to format "O_i" with "i" as the changing variable. You can use the following to make the variable name before storing it. ie

eval(['O' num2str(i) ' = O (' num2str(i) ', n )']) ;
FirstName LastName
  • 1,891
  • 5
  • 23
  • 37
  • Sorry, but nothing you wrote is valid in matlab. Please test a line of code before you post it. – John Sep 25 '13 at 17:12