How to pick and subtract specific elements in matrices

1 次查看(过去 30 天)
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!

采纳的回答

Matt J
Matt J 2018-9-6
编辑:Matt J 2018-9-6
[matches,Ia,Ic]=intersect(a,c);
differences=b(Ia)-d(Ic)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by