Search code examples
pythonmatlabdata-structurescell

MATLAB Size equivalent function in python


hI i have a code in matlab. Based on my understanding I have translated that code to python. Can someone let me know if it is the right way to translate.

for i = 1:length(Filters)
    Filters{i} = gpuArray(2*(single(sign(Filters{i}))-0.5));
    NumLearntWeightsEachLayer(i) = size(Filters{i},3)*size(Filters{i},4)*4;
end
NumLearntWeightsEachLayer(end) = size(Filters{end},3)*size(Filters{end},4);
NumLearntWeightsEachLayer
TotalLearntWeights = sum(NumLearntWeightsEachLayer)

Could someone let me know if this could be an equivalent code for the for loop here.

for i in range (1,Filters):
    Filters(i) = (2* (Filters(i) - 0.5))
    NumLearntWeightsEachLayer(i) = (Filters(i),3).shape * (Filters(i),4).shape *4

I also want to know what could be the right translation for the last part of the code

 NumLearntWeightsEachLayer(end) = size(Filters{end},3)*size(Filters{end},4);

Solution

  • It's a good start.. some small fixes -

    for i in range (0,len(Filters)):
        for j in range(0, len(Filters[i]):
            Filters[i][j] = 2*(round(Filters[i][j],1) - 0.5)
        NumLearntWeightsEachLayer[i] = len(Filters[i][3])*len(Filters[i][4])*4
    

    For the last line-

    NumLearntWeightsEachLayer(end) = size(Filters{end},3)*size(Filters{end},4);
    

    It can be written as -

    NumLearntWeightsEachLayer[-1] = len(Filters[-1][3])*len(Filters[-1][4]);