Wrong square parentheses (square brackets)?

Asked

Viewed 396 times

1

I have this function in Matlab

cn = reshape(repmat(sn, n_rep, 1), 1,[]);

in Python I have the following code:

import numpy as np
from numpy.random import randint

M=2
N=2*10**8 ### valor de Dados
n_rep = 3 ## numero de repetições
sn = randint(0,M,size = N)### Inteiros 0 e 1
print("sn=", sn)
cn_repmat=np.tile(sn,n_rep)
print("cn_repmat=", cn_repmat)
cn=np.reshape(cn_repmat,1,[])
print(cn)

I’m not sure if the square parentheses in python are like this because it gives me the following error

  File "C:/Users/Sergio Malhao/.spyder-py3/Desktop/untitled6.py", line 17, in <module>
    cn=np.reshape(cn_repmat,1,[])

  File "E:\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 232, in reshape
    return _wrapfunc(a, 'reshape', newshape, order=order)

  File "E:\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 57, in _wrapfunc
    return getattr(obj, method)(*args, **kwds)

ValueError: cannot reshape array of size 600000000 into shape (1,)
  • 1

    That mistake is not about Parênteses, error of Parentheses that’s how it is SyntaxError: Missing parentheses

  • @Wéllingthonm.Download the rectal parenthesis is what we call a square bracket. Possibly the author is from another country. I believe the question is whether it is right to use [] in function reshape.

  • yes the doubt is if it is right to use brackets (parentheses) and how I can make the correct syntax, in cn

1 answer

1

The problem is syntax. You copied the code without looking at what the python function uses as input.

With a look at the manual it is possible to see that a direct copy Matlab->python does not work.

The mistake:

Valueerror: cannot reshape array of size 600000000 into Shape (1,)

is exactly because the format that was given as input is not accepted.

What you should use is

cn=np.reshape(cn_repmat,(Linhas,Colunas))

In your case, as the intention is to have only one line, the entrance -1 for the column value is ideal. This entry in this function has the same effect as the [] from Matlab, thus remaining:

cn=np.reshape(cn_repmat,(1,-1))

However, this function does not seem necessary given its code. Because the tile is just copying a matrix from a line sn n_rep times in the same line.

Looking at the data

Looking at the input/output of data from the initial expression we have:

a=1:4
reshape(repmat(a, 4, 1), 1,[])
ans =
[ 1  1  1  1  2  2  2  2  3  3  3  3  4  4  4  4 ]

python

b=np.array([1,2,3,4])
np.reshape(np.tile(b,(4,1)),(1,-1))
>array([[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]])

So this doesn’t work. However, just use the transposed matrix cn_repmat.T that it is in the same order as Matlab.

np.reshape(np.tile(b,(4,1)).T,(1,-1))
>array([[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]]) 

Another option is also to use ravel, which does exactly how you want to still have the option FORTRAN (order='F') that reads the matrix column-Wise :

np.tile(b,(4,1)).ravel(order='F')
>array([1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4])
  • And what modifications are these?

  • Guto would then be cn=np.reshape(1,cn_repmat)? I’ve been going over this for 3 hours, it might help me?

  • 1

    @Jeffersonquesado Is there :D !

  • 1

    Great answer!

  • @Sergionunes, here’s the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.