0

I was wondering how to create a matrix of n cols, where each column is a distribution of the manner 0:0.01:1 What I ideally want is a form of ndgrid with

[x1,x2,x3...,xn] = ndgrid(0:0.01:1,0:0.01:1.....m times) 

My constraint is the rows should add upto 1. That is I would only keep indices of those rows from the above grid which add up to 1. But as you can understand this is too large to compute. Hence, I am looking for short / efficient ways of achieving this. I would appreciate any help.

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
peacefrog
  • 69
  • 6
  • 1
    I have some troubles of understanding what you mean. You in case you are talking about multi dimensional matrices it is good to use key words as a N dimensional matrix, dimension 1 instead of row and so on. Please edit the question and clarify the parts where dimesions are involved so we can understand exactly what you need. That said, you cannot save the full matix anyway. Why not just define how the number how element _i_ relates to the matrix (preferably the same way that matlab indexes matrices with linear indexing) and iterating over a single vector? – patrik May 04 '16 at 06:45
  • 1
    What do you call `row` in a matrix with `n` dimensions? – BillBokeey May 04 '16 at 07:46

1 Answers1

0

If you just have a 2-dimensional matrix and want to filter it so only the rows that add up to 1 you can do so by summing and comparing all the rows and then using that as index as such:

>> A=[0.1,0.3,0.3,0.3; % adds up to 1
      0.5,0.4,0.1,0.4; % adds up to 1.2
      0.4,0.2,0.1,0.3; % adds up to 1
      0.0,0.1,0.5,0.0] % adds up to 0.6
>> A(sum(A,2)==1,:)

Where you sum(A,2) sums A over the 2nd dimension (rows), compares this sum to 1. This gives a boolean array which is used as index to select the rows of the matrix which then gives:

ans =
  0.1000    0.3000    0.3000    0.3000
  0.4000    0.2000    0.1000    0.3000
Swier
  • 4,047
  • 3
  • 28
  • 52