필터 지우기
필터 지우기

How to find corresponding value in two arrays?

조회 수: 2 (최근 30일)
Andrejus
Andrejus 2014년 10월 22일
편집: Andrejus 2014년 10월 22일
Hi,
I have to data arrays, A and B. I want to knwo how it is possible to find corresponding number or value of the element in B array when accumulation in A array reaches certain value? Basically, I want to elements in array A until the sum reaches prescribed value. Then I need to know what was the corresponding value in B array when the condition is met. How can I do this? Cannot find a way my own...
Thanks

채택된 답변

Chad Greene
Chad Greene 2014년 10월 22일
편집: Chad Greene 2014년 10월 22일
A = rand(1,100);
B = rand(1,100);
MyBElements = B(cumsum(A)<10)
Above, MyBElements is all the elements in B until the cumulative sum of A reaches 10.
Plotting the cumulative sum of A, a threshold at 10, and the individual elements of A, B, and MyBElements can be done like this:
figure;
subplot(2,1,1)
plot(cumsum(A));
hold on
plot([1 100],[10 10],'r')
axis tight
title('cumulative sum of A')
subplot(2,1,2)
plot(1:100,A,'b+',1:100,B,'rx')
hold on
box off
plot(MyBElements,'ro')
legend('A','B','MyB')
  댓글 수: 1
Andrejus
Andrejus 2014년 10월 22일
편집: Andrejus 2014년 10월 22일
Thanks, that worked great. I have spent 2 days for this and you solved easily. Super thanks!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by