Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Subtract an array from another array based on elements in both columns

I am looking for an efficient way to do this:

(a=1 2; 1 3; 2 3)  - b=(1 2; 2 3) = (c=1 3)

or:

a=nchoosek([1 2 3 4 5 6],2) - b=(1 2; 1 3; 2 4;3 5;3 6) = (c=1 4;1 5;1 6;2 3;2 5;2 6;3 4;4 5;4 6; 5 6)

a and b will be given and I want to obtain c; all data is in double. There will always be two columns.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I am looking for an efficient way to do this:

(a=1 2; 1 3; 2 3)  - b=(1 2; 2 3) = (c=1 3)

your notation is a bit inconsistent, but it looks you want to understand a and b as a set of tuples of two numbers. That’s fine, do just exactly that.

In Python, this would be super easy; I’m only using Python to illustrate the concept¹:

a = {(1,2), (1,3), (2,3)}
b = {(1,2), (2,3)}
c = a - b

In matlab, setdiff is to be used; basically, you need to have the 2-tuples as rows of a and b (so a will be a 3×2 matrix).


¹ That’s not 100% true. I’m also here to tell you that if your code depends on Matlab doing a lot of set operations, then you will have a long, slow day of annoying coding, and another one of slow, annoying matlab running. It’s really not something matlab is good at, neither from the development perspective, nor from the execution side. Might be worth looking at other tools than matlab!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading