-3

I have a matrix of steady state distributions and this is how the results are coming out.

[[ 0.43397114,  0.00939583,  0.55663303]
[ 0.43397114,  0.00939583,  0.55663303]
[ 0.43397114,  0.00939583,  0.55663303]]

I want to select one of them as P[0] for instance. But it's coming out as

[[ 0.43397114,  0.00939583,  0.55663303]] 

Can anyone tell me how I can select and convert one of them as they are the same so that it can plotted against let's say X = np.linspace(100,100,3)

Anthony Pham
  • 3,096
  • 5
  • 29
  • 38
Eric Enkele
  • 183
  • 1
  • 7
  • 17
  • 5
    Your original pasted code is syntactically incorrect. Please fix it first. Hint: It needs at leas 8 commas, maybe more, – DYZ Jul 31 '17 at 22:00
  • What is it that you want to see? – Batman Jul 31 '17 at 22:05
  • I just want to pick one of the distribution and plot it against X. basically the double brackets issue is causing a problem. – Eric Enkele Jul 31 '17 at 22:06
  • It looks like you're using `np.matrix`. If you never use that again, then this type of problem will go away – Eric Jul 31 '17 at 22:29

4 Answers4

1

Ok I will just assume that maybe what you want is to make list from list of lists.

In [1]: import numpy as np

In [2]: P = [[ 0.43397114,  0.00939583,  0.55663303],
   ...: [ 0.43397114,  0.00939583,  0.55663303],
   ...: [ 0.43397114,  0.00939583,  0.55663303]]

In [3]: P = np.array(P)

In [4]: P = P.flatten()

In [5]: P
Out[5]:
array([ 0.43397114,  0.00939583,  0.55663303,  0.43397114,  0.00939583,
        0.55663303,  0.43397114,  0.00939583,  0.55663303])

Now you can plot them easily. I hope this is what you meant.

user3053452
  • 640
  • 1
  • 12
  • 38
1

I expect this kind of behavior if you have created a np.matrix:

In [37]: mat=np.matrix([[ 0.43397114,  0.00939583,  0.55663303]
    ...: ,[ 0.43397114,  0.00939583,  0.55663303]
    ...: ,[ 0.43397114,  0.00939583,  0.55663303]])
    ...: 
In [38]: mat
Out[38]: 
matrix([[ 0.43397114,  0.00939583,  0.55663303],
        [ 0.43397114,  0.00939583,  0.55663303],
        [ 0.43397114,  0.00939583,  0.55663303]])
In [39]: mat[0]
Out[39]: matrix([[ 0.43397114,  0.00939583,  0.55663303]])

That array subclass preserves 2d dimensionality when indexed.

If it had been an array, the indexed row would be 1d

In [42]: mat.A[0]
Out[42]: array([ 0.43397114,  0.00939583,  0.55663303])

But I'm puzzled as to how you got that particular display. There's no matrix(. There are commas in the inner lists, but not between the outer ones. If real, the could mean you have an array of lists, not a 2d. But then I wouldn't expect that indexing behavior.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
0

It looks like you're trying to use a 2D or nested list.

You can create it with the following:

p = [[ 0.43397114,  0.00939583,  0.55663303],
   [ 0.43397114,  0.00939583,  0.55663303],
   [ 0.43397114,  0.00939583,  0.55663303]]

p[0] will access the first array which is [ 0.43397114, 0.00939583, 0.55663303]

p[0][0] will access the first element of the first array which is 0.43397114

Simon
  • 855
  • 9
  • 24
  • I thought there could have been a single line to do it. So I should access each element then create a new lists? – Eric Enkele Jul 31 '17 at 22:08
  • There's no need to create a new list. Just understand that the first object returned when you do `p[0]` is already a list. Then access the element you want as you normally would for a list. – Batman Jul 31 '17 at 22:11
  • What you've written in your initial question does not make much sense to me. Could you please rephrase it without using any pronouns with only one direction per sentence so that I know exactly what you are trying to do? @Eric Enkele – Simon Jul 31 '17 at 22:12
0

Your "matrix" is really just a list of lists. So when you say p[0] what you're really doing is accessing the first member of the list. Which, in this case is the list. If you then wanted to get the first element, for example, of that list you could do p[0][0]. Or p[0][1]. Or p[2][2], etc.

Also, you can use a numpy.matrix do do this.

import numpy as np

p = np.matrix("1, 2, 3; 4, 5, 6; 7, 8, 9")
foo = p.item(0)
Batman
  • 8,571
  • 7
  • 41
  • 80