alternate way , without ismember function

Hi
I am having problems in using the ismember function, so I want to replace ismember and I need help in replacing the function.
Below is example code
INData = [
0.0120849609375
0.014190673828125
0.015838623046875
0.01214599609375
0.012481689453125
0.0123291015625
0.621002197265625
0.017181396484375
0.015167236328125
0.015350341796875
0.014556884765625
0.01739501953125
0.621002197265625
0.023162841796875
0.017578125
0.01361083984375
0.01239013671875
0.012115478515625
0.012451171875
0.01702880859375
0.01214599609375
0.012725830078125
0.0125732421875
0.012603759765625
0.012359619140625
0.014190673828125
0.013214111328125
0.013427734375
0.01531982421875
0.621002197265625
0.01239013671875
0.013092041015625
0.017791748046875
0.015960693359375
];
dMark = [7 13 30];
s_data = ismember(1:size(INData(:,1),1),dMark);
s_data =
1×34 logical array
0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
I need help in replacing isemember function.
Thank you

 Accepted Answer

The ismember function is doing exactly what you told it to do! What’s the problem?

20 Comments

I don't want to use ismember function . This is causing problem while using the "coder", so I need replace code for ismember function!
What do you want the function to do?
I want a code that perform same as ismember function which is supported by the Matlab coder as well.
This appears to work, and at least here, produces the same results as ismember:
s_data = sum(1:size(INData(:,1),1) == dMark(:));
and to produce a logical vector:
s_data = sum(1:size(INData(:,1),1) == dMark(:)) == 1;
With respect to the Coder, the documentation states:
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
So apparently under certain dircumstances, it can be used with Coder. (I do not have the coder, so I cannot experiment with functions using it.)
Thank you!
I am testing the solution...I'II feedback
My pleasure!
It worked when I tested it.
EDIT — (8 Oct 2020 at 13:26)
Since the first vector must be a row vector and the second a column vector, this slight change enforces that so it will work regardless of the original vector orientations:
s_data = sum(reshape((1:size(INData(:,1),1)),1,[]) == dMark(:)) == 1;
.
I tested with coder , I see dimension mismatch issue
Size mismatch (size [1 x 34] ~= size [:? x 1]).
Need to resolve...
But in case of below solution from Ameer Hamza - this doesn't throw error :-)
s_data = false(1, size(f(:,idx), 1));
s_data(locs) = 1;
Can you please help me in understanding what is the difference between your proposal and below one..
Thank you!
Let me test your new proposal
s_data = sum(reshape((1:size(INData(:,1),1)),1,[]) == dMark(:)) == 1;
That is not my code.
My code is:
s_data = sum(reshape((1:size(INData(:,1),1)),1,[]) == dMark(:)) == 1;
and it should run without error.
For reference purpose , please see the error when plugged back to real code..
Can you confirm the dimension on right size when you tested and it passed ?
Thank you!
My code works when I test it. I do not have the Coder, so I cannot test my code with it.
Perhaps the Coder does not recognise the (:) subscript convention that forces a column vector.
Try this, using reshape for both vectors. (Again, it runs without error and produces the correct result when I test it in R2020b.):
s_data = sum(reshape((1:size(INData(:,1),1)),1,[]) == reshape(dMark,[],1)) == 1;
.
Sorry- This does not work Size mismatch.
If you test without Matlab coder then there is no problem.....
It is working for below case
The Coder must not be able to do that comparison. As I mentioned, I have no experience with the Coder, since I do not have it or have access to it.
I am still doing my best to see if I can come up with something the Coder likes, since it apparently also does not do implicit expansion.
Try this:
s_data = sum(bsxfun(@minus, reshape((1:size(INData(:,1),1)),1,[]), reshape(dMark,[],1)) == 0) == 1;
.
Great, Thanks a ton for efforts. You provided the right solution
It will be helpful for me if you can explain your thougths and share the explanation
Sure ,
There is a comment , I see side effect in form of overflow , do you know why ?
Overflow error in expression 'bsxfun(@minus, reshape((1:size(f(:,1),fi(1, 0, 1, 0, fm))),1,[]), reshape(locs,[],1))'. Percentage of Current Range = 130%.
My pleasure!
The Coder apparently does not do autoimatic explicit expansion, as MATLAB has since R2016b. In order to get the correct result, I subtracted the second argument from the first, using bsxfun, that does the expansion, so that both arguments become the same size. (There are other ways to do that, however bsxfun is the fastest and most efficient way.) The resulting matrix is 0 where the element values are the same, so the first logical operation returns a matrix where the values that are 0 as true (or 1), and using sum over the columns produces the desired vector. This creates a numeric vector that becomes a logical vector with the second logical operation, setting the elements of the vector to true if they are equal to 1. The result is the desired logical vector.
Thanks a lot for all the efforts and help. I am greatful for your explantion as well.
As always, my pleasure!
This was an interesting problem!

Sign in to comment.

More Answers (1)

An alternative approach
dMark = [7 13 30];
s_data = false(1, size(INData, 1));
s_data(dMark) = 1;

3 Comments

This doesn't work,
Please check the Output of s_data
K>> s_data(dMark)
ans =
1×3 logical array
1 1 1
expected out is
s_data1 =
1×34 logical array
Columns 1 through 32
0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
Columns 33 through 34
0 0
Don't check the output of s_data(dMark). Just run
s_data
after running my code.
Your interpretation is right s_data give right output!

Sign in to comment.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!