How to pick and subtract specific elements in matrices

1 visualizzazione (ultimi 30 giorni)
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!

Risposta accettata

Matt J
Matt J il 6 Set 2018
Modificato: Matt J il 6 Set 2018
[matches,Ia,Ic]=intersect(a,c);
differences=b(Ia)-d(Ic)

Più risposte (0)

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by