MATLAB: Can you pls explain me the for loop here,how is it working and how is this code working >>[~, indx] = find(wd >= wd(1)*0.7)

MATLAB

function height = getHeight(signal)
maxVal = max(signal);
wd = [];
for nn = 1 : maxVal
wd(nn) = getSignalWidth(signal, nn);
end
[~, indx] = find(wd >= wd(1)*0.7);
height = indx(end);

Best Answer

The tilde, ~, is used to ignore function outputs or inputs. the two outputs version of find returns the rows and columns where the elements are found, and in this case, the author of the code was only interested in the column, so ignored the row.
The whole thing is completely unnecessary in this case, though. Since wd is a row vector, the row is always going to be 1 (so, yes, it makes sense to ignore it) and the column is always to be the same as the index returned by the single output form of find. That is, the code could have been written as:
indx = find(wd >= wd(1)*0.7);
with absolutely no difference in functionality.
While you're at it, you may want to change the allocation of wd from
wd = [];
to
wd = zeros(1, maxVal);
As it is the code will cause resizing and reallocation of wd on each loop iteration, whereas my version preallocates a variable of the right size.
Overall, I give poor marks to whoever wrote that code.