필터 지우기
필터 지우기

How to pick and subtract specific elements in matrices

조회 수: 1 (최근 30일)
g
g 2018년 9월 6일
편집: Matt J 2018년 9월 6일
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