kindly explain me these two lines of code

Status
Not open for further replies.

moonnightingale

Full Member level 6
Joined
Sep 17, 2009
Messages
362
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,298
Activity points
3,832
Kindly explain me these 2 lines of code

for i=1:size(S,1) % i loop continues till it gets size of row of matrix S

[tf, loc] = ismember(S(i,: ),SEP,'rows'); % WHAT IS FUNCTION OF LOC

D(i,: ) = EP(loc,: ); % KINDLY EXPLAIN THESE TWO LINES

end

I know ismember compares each row of matrix S with each row of SEP and returns 0 if they are not same and 1 if they are same. But what is function of tf and LOC there
 

In MATLAB help it is stated that
[tf, loc] = ismember(A, S, ...) returns an array loc containing the highest index in S for each element in A that is a member of S. For those elements of A that do not occur in S, ismember returns 0.
According to the statement above, in your case:

tf will be equal to 1 if there exist a row in SEP that is equal to S(i,: ), and 0 otherwise.

loc will be the index of the row of SEP which is equal to S(i,: )

For example:

if SEP = [1 2 3; 4 5 6; 7 8 9] and S(i,: ) =[4 5 6]
then loc will be equal to 2, because the 2nd row of SEP is equal to S(i,: ).

if S(i,: ) = [7 8 9] then loc will be 3, because the 3rd row of SEP is equal to S(i,: ).

And as for the logical value "tf"

You need to use it because the ismember() function doesn't have any prototype which returns only the index (i.e. loc).

It returns either only tf, or both tf and loc. So in order to get the index, you have to get the logical value also, even though you won't be using it.

If you still can't understand it, then debug your code and see the values these two variables take.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…