0

I'm converting matlab code to python, and I'm having a huge doubt on the following line of code:

BD_teste = [BD_teste; grupos.(['g',int2str(l)]).('elementos')(ind_element,:),l];

the whole code is this:

BD_teste = [];
por_treino = 0;
for l = 1:k
    quant_elementos_t = int64((length(grupos.(['g',int2str(l)]).('elementos')) * por_treino)/100);
    for element_c = 1 : quant_elementos_t
        ind_element = randi([1 length(grupos.(['g',int2str(l)]).('elementos'))]);
        BD_teste = [BD_teste; grupos.(['g',int2str(l)]).('elementos')(ind_element,:),l];
        grupos.(['g',int2str(l)]).('elementos')(ind_element,:) = [];
    end
end

This line of code below is a structure, as I am converting to python, I used a list and inside it, a dictionary with its list 'elementos':

'g',int2str(l)]).('elementos')

So my question is just in the line I quoted above, I was wondering what is happening and how it is occurring, and how I would write in python.

Thank you very much in advance.

1
  • I'm writing an answer, but as general advice, avoid naming a variable l it is easy to confuse it with 1. Commented Oct 18, 2019 at 20:32

1 Answer 1

1

BD_teste = [BD_teste; grupos.(['g',int2str(l)]).('elementos')(ind_element,:),l];

Is one very weird line. Let's break it down into pieces:

int2str(l) returns the number l as a char array (will span from '1' until k).

['g',int2str(l)] returns the char array g1, then g2 and so on along with the value of l.

grupos.(['g',int2str(l)]) will return the value of the field named g1, g2 and so on that belongs to the struct grupos.

grupos.(['g',int2str(l)]).('elementos') Now assumes that grupos.(['g',int2str(l)]) is itself a struct, and returns the value of its field named 'elementos'.

grupos.(['g',int2str(l)]).('elementos')(ind_element,:) Assuming that grupos.(['g',int2str(l)]) is a matrix, this line returns a line-vector containing the ind_element-th line of said matrix.

grupos.(['g',int2str(l)]).('elementos')(ind_element,:),l appends the number one to the vector obtained before.

[BD_teste; grupos.(['g',int2str(l)]).('elementos')(ind_element,:),l] appends the line vector [grupos.(['g',int2str(l)]).('elementos')(ind_element,:),l] to the matrix BD_teste, at its bottom. and creates a new matrix.

Finally: BD_teste = [BD_teste; grupos.(['g',int2str(l)]).('elementos')(ind_element,:),l];``assignes the value of the obtained matrix to the variableBD_teste`, overwriting its previous value. Effectively, this just appends the new line, but because of the overwriting step, it is not very effective.

It would be recommendable to append with: BD_teste(end+1,:) = [grupos.(['g',int2str(l)]).('elementos')(ind_element,:),l];

Now, how you will rewrite this in Python is a whole different story, and will depend on how you want to define the variable grupos mostly.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.