Nimrodshn
Nimrodshn

Reputation: 971

Matlab to C++ conversion

I'm trying to write a program in C++ based on matlab code, but i dont understand some of the syntax and i'm not used to dynamic programming -

[~, min_index] = min(new_energy(min_l,L_a(min_l):L_b(min_l)));
min_index = L_a(min_l) + min_index - 1; % correct index in the entire image

where 'new_energy' is a matrix.

my questions are :

1.how do you find the min of one term(in the first line)? it seems as if i don't understand this : notation.

2.also what does [~,min_index] mean?

Upvotes: 1

Views: 127

Answers (1)

sgarizvi
sgarizvi

Reputation: 16816

In MATLAB, the colon notation : means we are referring to a range of values or simply an array of values.

Colon operator is used to generate a sequence of values e.g. 1:5 will return an array [1 2 3 4 5]. Read more about colon operator here.

So L_a(min_l):L_b(min_l) is a range of values. e.g suppose if the value of L_a(min_l) is 10 and value of L_b(min_l) is 20, then the resultant range would be:

[10 11 12 13 14 15 16 17 18 19 20].

Now, parentheses operator (index operator) on a matrix is used to access a submatrix of a larger matrix. It takes a single integer or a range of integers to specify the range of submatrix we want to access. So in the following line

new_energy(min_l, L_a(min_l):L_b(min_l))

We are selecting a submatrix from new_energy matrix. That submatrix is actually the row number min_l of the matrix and all the columns specified by the range as I explained before. So by executing this statement, we are getting a single row of the matrix as output.

Next we are executing the min function of MATLAB on the row we just extracted.

A variant of the min function of MATLAB can return the minimum value as well as the index of that minimum value in a range.

[minValue, indexOfMinValue] = min(...);

If a MATLAB function returns multiple values, and we specify ~ in place of a return variable, it means we do not need that result and that value is not returned by the function.

[~,min_index] means we are getting only the index of minimum value and not the minimum value itself as we have specified that we do not need it.

Upvotes: 3

Related Questions