0

I have been converting some Matlab code into Python. I have been debugging this program for a while and I am wondering if I am correct in the way I am thinking about the arrays in Matlab.

I have this code in Matlab (edited to add missing variables and executions):

pdf_pars = pdf_pars(1)*ones(1,p);
weights = 1;

% determine  the size of each lenght
mode_length = round(weights*samp_size);

% correct for rounding erros
mode_length(no_modes) = mode_length(no_modes) + samp_size - sum(mode_length) ;

for i=1:no_modes
    x = [x dirichlet(pdf_pars(i,:),mode_length(i))']; 
end 

When you read the first line, it seemed to me like mode_length was an integer (or perhaps a double). However the next line starts indexing. I know that in Python, this treatment of mode_length will cause an error.

Am I interpreting this Matlab code correctly? And how can I work around this as I continue moving code into Python?

7
  • 4
    What exactly are weights, samp_size, and no_modes? Commented Aug 9, 2018 at 18:03
  • Judging by the variable names, weights is a vector, and samp_size is a scalar. Commented Aug 9, 2018 at 18:15
  • @rahlf23, the code is from an open source paper. samp_size is one of the required inputs and it is described as the "number of spectral vectors to be generated". So I believe it is a scalar. Weights is also a scalar. Commented Aug 9, 2018 at 18:42
  • 2
    If weights and samp_size are both scalars (which I'm skeptical is the case), then mode_length will be a scalar. The reason for my skepticism is because you then try to index mode_length in the following line... Commented Aug 9, 2018 at 18:45
  • 1
    The important value is really no_modes because this is the index you are trying to query Commented Aug 9, 2018 at 18:46

2 Answers 2

2

round(X) in Matlab rounds each element of X. If you feed it a vector, it returns a vector.

Sign up to request clarification or add additional context in comments.

Comments

2

Without knowing what weights, samp_size, and no_modes are, this is somewhat speculative.

With that being said, round() in MATLAB will retain the size that is passed as an input. Therefore, if you pass a double, then it will round the double and return a double. Likewise, if you pass a vector, then MATLAB will perform the rounding element-wise and return a vector.

So assuming that weights is a vector and samp_size is a scalar, then you are applying round() to each element of the resulting vector from weights*samp_size, thus mode_length will be a vector.

Then it looks like you are modifying the element in mode_length that corresponds to index no_modes, and simply adding samp_size and subtracting sum(mode_length), both of which are scalar values.

Something to note if you are not aware: MATLAB indexing begins at 1, whereas Python indexing begins at 0.

1 Comment

I clarified what some of the variables were. Weights and samp_size are both scalars. At least in the call that I make that results in these errors, weights has a value of 1.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.