Home > Software design >  How can I concatenate the following python code as it done in Matlab?
How can I concatenate the following python code as it done in Matlab?

Time:09-29

I am trying to run this MATLAB code in Python:

Y1=2;
YN=6
N=4;
n=(1:(N-1)).';
y=[(YN-Y1)/N.*n Y1;YN];
y =

     3
     4
     5
     6

y=3 4 5 6 is my answer in Matlab

The same code I am trying in Python which give me this results which is the same but I cannot concatenate y2.

N,y1,y2=4,2,6
n=np.arange(1,N)
y =((y2-y1)/N*n y1)
print(y)
answer--> array([3., 4., 5.])

CodePudding user response:

In numpy, you want to use specific fucntions, it does not have an easy way like MATLAB's [arr1, arr2]. Note that this is just a shortcut of writing the cat() function in MATLAB.

Use one of the followings: np.concatenate (for the general case, equivalent to cat()) or np.vstack ([arr1;arr2]), np.hstack([arr1,arr2]), np.dstack (no MATLAB shortcut for this one) for specific dimensions.

CodePudding user response:

Simple, you have to use np.append():

y = np.append(y,y2)
  • Related