Python Forum
Simple Matrix Assignment - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Simple Matrix Assignment (/thread-20257.html)

Pages: 1 2


Simple Matrix Assignment - m_llaa - Aug-02-2019

Dear everybody
I am very expert with MATLAB programming but I am new with Python.
Very simple matrix assignment which is worked well in MATLAB, does not work in Python.
import numpy as np

x = np.zeros([100,2,20])
y = np.random.binomial(1,0.5,size=[100,1,20])

x[:,0,:] = y
Please help.
P.S. Please don't change the dimension of x and y. I need both of them 3D.


RE: Simple Matrix Assignment - SheeppOSU - Aug-02-2019

I don't get what you are trying to do on line 6. Could explain that part?


RE: Simple Matrix Assignment - m_llaa - Aug-03-2019

(Aug-02-2019, 08:06 PM)SheeppOSU Wrote: I don't get what you are trying to do on line 6. Could explain that part?

Simply, I am trying to copy y in the first column of x
Please note that x and y are 3D matrices.
x has 100 rows, 2 columns and 20 channels
y has 100 rows, 1 column and 20 channels


RE: Simple Matrix Assignment - SheeppOSU - Aug-03-2019

I don't know for sure, but try x[0] = y[0]


RE: Simple Matrix Assignment - paul18fr - Aug-03-2019

oups Blush not awaked


RE: Simple Matrix Assignment - m_llaa - Aug-03-2019

(Aug-03-2019, 06:31 AM)SheeppOSU Wrote: I don't know for sure, but try x[0] = y[0]

wrong


RE: Simple Matrix Assignment - SheeppOSU - Aug-03-2019

Could you explain what the line 6 means as in, why are there colons and a zero in the middle surrounded by brackets. If i understand what it means, I can probably translate it to Python code


RE: Simple Matrix Assignment - paul18fr - Aug-03-2019

I guess the issue comes from the matrix size definition (the 3rd value seems not be the depth, isn't it?) , but I'm not familar with 3D arrays and I'm still missing some stuffs

Maybe can you find some tricks here

The following code is not you're expecting, but it may highlight some things
import numpy as np
 
x = np.zeros( (100,20,2) );
y = np.random.binomial(1, 0.5, size=[100,20,1]);
z = np.random.random((10, 20, 2));
x[:, :, 0] = y[:, :, 0];

print(x[:,:,0])
Paul


RE: Simple Matrix Assignment - m_llaa - Aug-04-2019

(Aug-03-2019, 08:55 AM)SheeppOSU Wrote: Could you explain what the line 6 means as in, why are there colons and a zero in the middle surrounded by brackets. If i understand what it means, I can probably translate it to Python code

The 2nd dimension of x has 2 elements. I want to fill the first element by y.


RE: Simple Matrix Assignment - SheeppOSU - Aug-04-2019

(Aug-04-2019, 11:49 AM)m_llaa Wrote: The 2nd dimension of x has 2 elements. I want to fill the first element by y.
The first element of the second dimension would look like "x[1][0]". 1 For the second dimension and 0 to get the first element. When I try "x[1][0] = y", I get an error. I don't get an error if I do "x[1][0] = y[1][0]", so is this what you are trying to do.