0

I'm trying to cnvert this line to python:

            xx=wqtran((nn-1)*N*2/K+nN,(mm-1)*N*2/K+nN,kk-1);

both (nn-1)*N*2/K+nN and (mm-1)*N*2/K+nN are [[1..128]] meaning array of 2D array with the shape of (1,128)

I've translated the last row to this:

        firstAxis = round(dot(dot((nn - 1), N), 2) / K + nN)
        secondAxis = round(dot(dot((mm - 1), N), 2) / K + nN)
        xx=wqtran[firstAxis-1, secondAxis-1, kk - 2]

as python counts from 0 instead of 1 and it doesn't support float indices (auto conversion).

the problem that xx is different with the 2 codes: in matlab xx is (128,128) matrix. while in python it's (1,128) matrix. Does anyone know where xx lost in translation?

Python xx

array([[-452.58030867-676.77261453j, -195.12401501-661.388484j  ,
        -174.14069268-609.99929489j, -132.25284621-580.72939701j,
        -148.73799066-547.11073351j, -159.38166597-546.81537669j,
        -161.87484594-539.42802218j, -169.35077334-545.3686585j ,
        -180.50911641-544.03823644j, -197.29690441-557.23079163j,
        -196.51746545-567.5090211j , -213.84197505-581.57152941j,
        -226.74628312-623.65644362j, -200.66624603-651.50540145j,
        -177.38731357-655.67501859j, -180.69705033-675.17325716j,
        -155.81407249-676.37138946j, -157.96308479-695.13212395j,
        -108.8650474 -736.25936469j,  -87.578376  -700.20224727j,
         -61.02036179-666.00742621j,  -64.97015   -638.36503754j,
        -101.50993294-656.93949503j,  -93.54775041-647.98566233j,
        -107.68397683-697.7795849j ,  -75.53171122-657.14821992j,
         -85.23769943-688.35238679j,  -92.83159059-686.44693563j,
         -71.43133014-697.60140338j,  -81.08785253-711.97445565j,
         -44.87630158-720.17033913j,  -52.44268065-722.02504979j,
          -5.25277497-740.89853418j,  -17.6212513 -686.37476794j,
         -29.2393824 -726.59904826j,    7.16364259-719.91709074j,
           5.90517312-717.18410487j,   22.8522649 -699.23464182j,
          18.63531638-715.0072984j ,   36.32674701-693.05814042j,
          26.7880449 -694.37681242j,   27.16655687-689.18079609j,
          27.85147304-711.44759484j,   61.06491269-720.79713687j,
          52.0941088 -706.34408308j,   75.35650028-725.46873301j,
          98.57388416-721.28197269j,  145.06602719-711.59314229j,
         141.991647  -664.74456743j,  150.71281574-653.53592807j,
         125.90978132-630.55654499j,  132.47112733-629.17946652j,
         125.51837461-616.7203905j ,  122.58396678-619.14956992j,
         142.70895126-614.49204458j,  159.16589703-593.52244992j,
         138.31128655-582.53972214j,  153.12963358-569.54241982j,
         165.29233736-545.01929937j,  154.13129768-471.78284923j,
          43.56773616-487.8220431j ,   54.25437947-524.72605456j,
          42.79114751-545.50768074j,   53.15219565-559.27877631j,
          43.76155234-560.12707983j,   54.6208528 -568.90568531j,
          56.97801932-575.17701242j,   65.2143702 -583.69413932j,
          73.23340404-580.67369009j,   79.07999037-613.44166576j,
         143.69183821-585.69001336j,  117.20653662-526.52470153j,
         100.57804234-536.03686963j,  108.44277397-538.21328614j,
          90.05264023-528.34900258j,   78.72868304-505.73274985j,
          69.97859571-513.46003129j,   66.93475688-526.93461531j,
          48.55950859-530.81706511j,   48.90034874-555.12529778j,
          96.22558483-565.6267038j ,  102.4548864 -523.24947975j,
          79.69038208-529.98772291j,   87.95211951-510.26008958j,
          89.69574996-535.75298725j,   90.254945  -499.1893208j ,
          78.94021112-511.87269866j,   81.67490639-504.04487668j,
          68.79809877-503.89951716j,   77.24724759-514.19868479j,
          58.04074534-515.98187084j,   98.08396247-507.92887948j,
         100.20824611-495.01481833j,   72.69918626-489.42866776j,
          76.7185985 -482.68643198j,   83.92155433-498.9687003j ,
          82.67280296-451.74266885j,   75.86185607-490.23454708j,
          57.94986269-458.47283805j,   41.1146784 -479.67628776j,
          78.93166632-478.31596167j,   51.50738234-477.3396975j ,
          78.20463698-494.72210057j,   85.75196528-469.34299269j,
          61.02017345-467.76114977j,  106.63119269-448.17563021j,
          84.6209639 -460.80005868j,  132.04063304-453.84619813j,
         130.40574008-287.10743659j,   -6.2580409 -359.45507084j,
          -5.28309533-400.4593048j ,    8.32020675-420.38420677j,
          12.90232396-425.91811269j,   55.04695631-409.77248592j,
          48.69647754-392.02448157j,   45.78594751-370.02692246j,
          50.11015329-338.79344838j,   -8.11827553-322.21361504j,
           3.22840585-285.08547853j,  -69.03273845-292.52545131j,
         -92.47239304-306.54784527j, -110.46527378-324.81171773j,
        -118.13552928-282.0133424j , -156.30293279-316.89599142j,
        -205.25063008-284.99760596j, -262.9615674 -320.56291071j,
        -297.34446645-364.17111847j, -388.19490322-330.83950936j]])

matlab xx too big for the post first rows:

  Columns 1 through 5

  -4.5258 - 6.7677i  -2.8199 - 6.5171i  -2.5924 - 6.3501i  -2.2652 - 6.1454i  -2.1380 - 6.0565i
  -3.3526 - 7.0387i  -1.9512 - 6.6139i  -1.8210 - 6.3541i  -1.5917 - 6.1802i  -1.5068 - 6.0629i
  -2.9576 - 6.8487i  -1.7324 - 6.2677i  -1.7414 - 6.1000i  -1.5113 - 5.9629i  -1.4118 - 5.8737i
  -2.5942 - 6.5328i  -1.6652 - 6.0255i  -1.5915 - 6.0017i  -1.3225 - 5.8073i  -1.2911 - 5.6604i
  -2.7038 - 6.1171i  -1.8519 - 5.7833i  -1.6694 - 5.7016i  -1.5398 - 5.4927i  -1.4874 - 5.4711i
  -3.0414 - 6.0944i  -2.0874 - 5.8841i  -1.9274 - 5.7351i  -1.7284 - 5.6416i  -1.6399 - 5.4976i
  -3.1291 - 6.2552i  -2.1179 - 5.9566i  -1.9873 - 5.8116i  -1.7679 - 5.6209i  -1.7752 - 5.5574i
  -3.1846 - 6.2824i  -2.2147 - 5.9888i  -2.0517 - 5.8205i  -1.9089 - 5.6169i  -1.8547 - 5.5742i
  -3.3634 - 6.3435i  -2.3585 - 6.0483i  -2.2724 - 5.8377i  -2.1611 - 5.7308i  -2.0719 - 5.6881i
  -3.4930 - 6.5714i  -2.4824 - 6.2474i  -2.4096 - 6.1046i  -2.1738 - 6.0056i  -2.0842 - 5.8660i
  -3.4977 - 6.6898i  -2.4959 - 6.3652i  -2.3771 - 6.2161i  -2.1896 - 5.9859i  -2.2244 - 5.9183i
  -3.7146 - 6.8227i  -2.6272 - 6.4702i  -2.5624 - 6.2801i  -2.4266 - 6.1418i  -2.3639 - 6.1235i
  -3.8749 - 7.2053i  -2.8709 - 6.7784i  -2.7552 - 6.6938i  -2.5841 - 6.5032i  -2.4996 - 6.4839i
  -3.7870 - 7.6706i  -2.6680 - 7.3165i  -2.4975 - 7.0619i  -2.3808 - 6.9107i  -2.2599 - 6.8245i
  -3.5470 - 7.9192i  -2.3884 - 7.3498i  -2.3778 - 7.1098i  -2.2237 - 6.9752i  -2.1495 - 6.8788i
  -3.3774 - 8.1407i  -2.3445 - 7.4918i  -2.3308 - 7.3440i  -2.1527 - 7.2085i  -2.0745 - 7.1216i
  -3.1525 - 8.2276i  -2.2111 - 7.6250i  -2.1091 - 7.5007i  -1.9231 - 7.3094i  -1.8284 - 7.2038i
  -3.0647 - 8.3645i  -2.0377 - 7.7732i  -1.9548 - 7.5233i  -1.8111 - 7.3576i  -1.7689 - 7.2205i
  -2.9077 - 8.4493i  -1.9463 - 7.7723i  -1.9293 - 7.6172i  -1.7293 - 7.4210i  -1.6972 - 7.3599i
  -2.8770 - 8.5876i  -1.8656 - 7.9865i  -1.7569 - 7.7734i  -1.6328 - 7.5554i  -1.5008 - 7.4696i
  -2.7261 - 8.8312i  -1.6047 - 8.0594i  -1.5835 - 7.8066i  -1.4221 - 7.5995i  -1.3657 - 7.4299i
  -2.5481 - 8.9706i  -1.4817 - 8.0861i  -1.4902 - 7.8014i  -1.4058 - 7.6098i  -1.3197 - 7.4890i
  -2.3348 - 9.2287i  -1.2996 - 8.1146i  -1.4435 - 7.9271i  -1.2240 - 7.7699i  -1.1770 - 7.5609i
  -1.9191 - 9.3370i  -1.1828 - 8.1255i  -1.2125 - 8.0338i  -1.0260 - 7.7334i  -1.0170 - 7.5347i
  -1.7057 - 9.2125i  -1.0760 - 8.1519i  -1.0698 - 7.9991i  -0.9242 - 7.6857i  -1.0191 - 7.4748i
  -1.5766 - 9.3942i  -0.9115 - 8.2014i  -0.9481 - 8.0558i  -0.8732 - 7.6749i  -0.9802 - 7.6137i
  -1.1969 - 9.5142i  -0.7600 - 8.2883i  -0.7354 - 8.0880i  -0.8064 - 7.7545i  -0.8295 - 7.6697i
  -0.6769 - 9.4567i  -0.4273 - 8.1623i  -0.5976 - 8.0069i  -0.6075 - 7.7987i  -0.6769 - 7.6913i
  -0.3850 - 9.0560i  -0.4248 - 8.0788i  -0.4050 - 8.0236i  -0.4633 - 7.7094i  -0.5513 - 7.7039i
  -0.4366 - 8.8908i  -0.2136 - 8.0561i  -0.2837 - 7.8554i  -0.3773 - 7.6979i  -0.3598 - 7.6238i
  -0.1777 - 8.9291i  -0.0988 - 7.8777i  -0.2033 - 7.8333i  -0.2561 - 7.5843i  -0.3953 - 7.5606i
  -0.0161 - 8.6390i  -0.1203 - 7.8495i  -0.1071 - 7.7569i  -0.2803 - 7.6176i  -0.2740 - 7.7102i
   0.1264 - 8.6460i   0.1332 - 7.8396i   0.0593 - 7.6877i  -0.0735 - 7.6183i   0.0476 - 7.6904i
   0.5057 - 8.2794i   0.2117 - 7.5596i   0.0900 - 7.5168i   0.0649 - 7.5802i   0.2925 - 7.4029i
   0.2960 - 7.7818i   0.1738 - 7.4925i   0.1958 - 7.4376i   0.2700 - 7.3344i   0.2059 - 7.0959i
   0.1925 - 7.8667i   0.3320 - 7.3270i   0.1611 - 7.1907i   0.1782 - 7.1215i   0.0658 - 7.0173i
   0.2259 - 7.5840i   0.1145 - 7.0688i   0.0032 - 7.1909i   0.0513 - 7.1091i   0.0311 - 7.0961i
   0.0885 - 7.5782i   0.0182 - 7.1834i   0.0570 - 7.2710i   0.2009 - 7.1118i   0.0902 - 6.9441i
   0.0255 - 7.4985i   0.0030 - 7.1903i   0.1157 - 7.2326i   0.0663 - 6.9297i  -0.0887 - 7.0950i
   0.1082 - 7.5894i   0.1059 - 7.2266i   0.2692 - 7.1582i   0.0889 - 7.0127i   0.2412 - 7.0495i
   0.1297 - 7.4225i   0.0920 - 7.1568i   0.1424 - 7.0073i   0.0280 - 7.0163i   0.0716 - 6.9444i
material bug
  • 147
  • 7
  • And `kk` is a scalar? With this indexing MATLAB returns a block. `numpy` is 'broadcasting' the 2 index arrays together. With 2 arrays the same shape like this that means it pairs the values - think of a diagonal instead of the block. You need to index with (128,1) and (1,128) shaped arrays to get the block. Soon or later you'll need to read some `numpy` indexing docs (not just wing it with a few simple rules-of-thumb). – hpaulj Oct 06 '21 at 15:34
  • in this iteration kk=2 – material bug Oct 06 '21 at 15:49

2 Answers2

1

This is advanced indexing in numpy:

In [30]: arr = np.arange(9).reshape(3,3)
In [31]: arr
Out[31]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

2 indexing arrays (same for display convenience):

In [32]: i, j = np.array([0,1,2]), np.array([0,1,2])
In [33]: arr[i,j]
Out[33]: array([0, 4, 8])

We have 2 arrays with shape (3,), and the result is also (3,), the 'diagonal' of the array. To do the same in MATLAB we have to use sub2ind or something like that to translate the index pairs into a flat index.

But if the first index is (3,1), then we get a (3,3) result, a block. (this is the default for MATLAB syntax):

In [34]: i1 = i.reshape(3,1)
In [35]: arr[i1,j]
Out[35]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
In [36]: i1,j
Out[36]: 
(array([[0],
        [1],
        [2]]),
 array([0, 1, 2]))

ix_ is convenient tool for generating the column vector i1:

In [37]: arr[np.ix_(i,j)]
Out[37]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

In numpy getting the diagonal and the block are doing the same thing, broadcasting the indexing array against each other.

The equivalent broadcasting with addition:

In [38]: 10*i+j
Out[38]: array([ 0, 11, 22])
In [39]: 10*i1+j
Out[39]: 
array([[ 0,  1,  2],
       [10, 11, 12],
       [20, 21, 22]])

With your (1,128) arrays, transpose would do the trick:

In [40]: I=i.reshape(1,3); J=j.reshape(1,3)
In [41]: I
Out[41]: array([[0, 1, 2]])
In [42]: arr[I,J]
Out[42]: array([[0, 4, 8]])
In [43]: _.shape
Out[43]: (1, 3)
In [44]: arr[I.T,J]
Out[44]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

I didn't use transpose with i, because it's 1d. The fact that MATLAB matrices are always 2d (or higher) and numpy can be 0 or 1d is one of hardest things for wayward MATLAB users to grasp.

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

If you do

array[rows, cols]

where rows and cols are lists/np.arrays, numpy will select the elements at

[[rows[0], cols[0]], [rows[1], cols[1]],..., [rows[n], cols[n]]]

If you're working with numpy arrays, the following should do what you want

xx=wqtran[(firstAxis-1)[:, np.newaxis], secondAxis-1, kk - 2]

A neater solution using np.ix_ has already been posted here

frippe
  • 1,329
  • 1
  • 8
  • 15
  • I'm using libsmop and when I use it I get this (after conersion via np.array): TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' – material bug Oct 06 '21 at 16:14
  • smop as in _small matlab and octave to python compiler_? You're not really providing any context about the error, but it seems unrelated so I think your code has other issues. Sorry for the formatting (rather lack thereof), here's a minimal example showing it in action in the python repl: >>> a = np.arange(3*3*2).reshape(3, 3, 2) >>> rows = np.array([0, 1]) >>> cols = np.array([1, 2]) >>> a[rows[:, np.newaxis], cols, 0] array([[ 2, 4], [ 8, 10]]) – frippe Oct 06 '21 at 16:46