Summing up two graphs with different starting point

조회 수: 9 (최근 30일)
Kushagra Mehrotra
Kushagra Mehrotra 2021년 3월 16일
댓글: Kushagra Mehrotra 2021년 3월 16일
Hey Matlab Community,
I have two graphs as seen from the figure below and I need to sum them up.
For the graph I have 4 separate column vectors. So for Red I have two separate column vectors with X and Y values and similarly for Blue I have two separate X and Y values The challenge is they both start from different point. The Red one starts at 25C and the Blue one starts at 170C. How can I plot the sum of graphs ? ( Just to clarify the resulting graph should be exactly equal to red graph until 170C and then it would start to deviate because of the addition of Blue.)
I hope I managed to explain the challenge I am facing. Any help would be greatly appreciated.
Thanks and Greetings
Kush

채택된 답변

ANKUR KUMAR
ANKUR KUMAR 2021년 3월 16일
편집: ANKUR KUMAR 2021년 3월 16일
Method 1: Using boolean
clc
clear
YY=rand(151,2);
XX=[[50:5:800]' [175:5:925]']
data=[XX(:,1) YY(:,1) XX(:,2) YY(:,2)]
plot(data(:,1), data(:,2),'k');
hold on
plot(data(:,3), data(:,4),'b');
uniq=unique([data(:,1);data(:,3)]);
for kk=1:length(uniq)
index1=find(data(:,1)==uniq(kk));
if ~isempty(index1)
value1(kk)=data(index1,2);
else
value1(kk)=nan;
end
index2=find(data(:,3)==uniq(kk))
if ~isempty(index2)
value2(kk)=data(index2,4);
else
value2(kk)=nan;
end
end
hold on
plot(uniq,value1+value2,'r')
Method 2: Interpolation
clc
clear
% generating random data
YY=rand(151,2);
XX=[[50:5:800]' [175:5:925]']
data=[XX(:,1) YY(:,1) XX(:,2) YY(:,2)]
plot(data(:,1), data(:,2),'k');
hold on
plot(data(:,3), data(:,4),'b');
uniq=unique([data(:,1);data(:,3)]);
value1=interp1(data(:,1),data(:,2),uniq);
value2=interp1(data(:,3),data(:,4),uniq);
plot(uniq,value1+value2,'r')
  댓글 수: 1
Kushagra Mehrotra
Kushagra Mehrotra 2021년 3월 16일
Ohh wow, this is incredible !
Thank you so much for taking time out and writing a code to help me, you are a saviour ! I will try to run the code and Accept the answer ASAP. Again thanks for the time you took to formulate that !

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by