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)

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);

5 Comments

wd(1).. first element is selected ............... wd(1)*0.7............find(wd > ans).. find how many numbers are greater than earlier answer.
check find command with a simple example separately. then solve this problem
Thank you sir, what does [~, indx] signifies in [~, indx] = find(wd >= wd(1)*0.7);
find() can return multiple outputs. Using it in the two-output form would normally be written as
[somevariable, indx] = find(wc >= wd(1)*0.7);
If you do not need the first result, here named "somevariable", then you can instruct MATLAB to throw the result away by not giving a variable name and instead using ~ . So
[~, indx] = find(wd >= wd(1)*0.7)
is the same as
[somevariable, indx] = find(wc >= wd(1)*0.7);
clear somevariable
Except that, since in this case wd is a vector, there is absolutely no point in using the two outputs form of find. The code is exactly identical to
indx = find(wd >= wd(1)*0.7);
Thanks to all for helping me,one more thing I am not understanding this line can you explain me, >> wd(nn) = getSignalWidth(signal, nn);

Sign in to comment.

 Accepted 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.

3 Comments

Thanks for helping me,you are really good all my doubts are cleared now thanks a lot,one more thing I am not understanding this line can you explain me, >> wd(nn) = getSignalWidth(signal, nn);
Yes sir I want to know how is this code running taking the other function getSignalwidth as mentioned by you
>>wd(nn) = getSignalWidth(signal, nn);

Sign in to comment.

More Answers (0)

Tags

Asked:

on 13 Jan 2016

Edited:

on 13 Jan 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!