Filter löschen
Filter löschen

How to pick and subtract specific elements in matrices

1 Ansicht (letzte 30 Tage)
g
g am 6 Sep. 2018
Bearbeitet: Matt J am 6 Sep. 2018
Let's say I have four sets of data (a,b,c,d). Arbitrary values chosen.
a = [1 2 3 4 5 6 7 8 9 10]
b = [0 4 2 8 5 5 6 2 1 9 ]
These are being plotted, with a being independent and b being dependent on a.
On the same plot, I have:
c = [2 5 7 9 10]
d = [4 4 8 2 5 ]
These are being plotted on the same graph, with c being independent and d being dependent on c.
My question is: I want to create a loop that subtracts b-d for the values where a=c. So I want to automate the process of something like: checking whether the value in a matches some value in c. If it does, then subtract the respective b-d. I also want to skip over/ignore values of a that do not match values in c. How can this be accomplished? (since the dimensions of the matrices are actually different, it would be great if this could work by just checking the values within the matrices).
So, in this case, one would get: [0 1 -2 -1 4] because the values of c (2, 5, 7, 9, 10) all match a value in a. Then, for these values that match, one subtracts the corresponding value in b-d. [4-4 5-4 6-8 1-2 9-5]
Thank you so much for the help!

Akzeptierte Antwort

Matt J
Matt J am 6 Sep. 2018
Bearbeitet: Matt J am 6 Sep. 2018
[matches,Ia,Ic]=intersect(a,c);
differences=b(Ia)-d(Ic)

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by