Comapre matrix and function a operation
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
A=[5 10 15 20 25] B=[1 1.5 2 4 8 14 17 25 18 16 22 23 17] the martix element B has to be compared with A to see between what range the fall and the lowest of the range has to be subtracted. for example 8 is between 5 and 10 it returns 8-5=3 25 is between 20 and 25 returns 25-20=5 Result:C=[1 1.5 2 4 3 4 2 5 3 1 2 3 2] tried with looping it is not fast enough. Kindly help me how to make it faster. Thanks!
채택된 답변
Fangjun Jiang
2016년 8월 29일
편집: Fangjun Jiang
2016년 8월 29일
Here is one solution. You might need to clarify some special cases. What the output should be if B contains value 25 or 20?
A=[5 10 15 20 25];
B=[1 1.5 2 4 8 14 17 25 18 16 22 23 17];
C=interp1(A,A,B,'linear')-interp1(A,A,B,'previous');
C(isnan(C))=B(isnan(C))
댓글 수: 9
Hems
2016년 8월 29일
If it is 25 then it should be 25-20=5 and for 20 20-15=5
Fangjun Jiang
2016년 8월 29일
편집: Fangjun Jiang
2016년 8월 29일
Adding the following code should be able to deal with the special cases where the element value in B matches the element value in A. Note that there is a new value 20 in B.
Still, there is one more exception. The code will have an error if B contains value 5 (the first value in A). Would such an exception exist?
A=[5 10 15 20 25];
B=[1 1.5 2 4 8 14 17 20 25 18 16 22 23 17];
C=interp1(A,A,B,'linear')-interp1(A,A,B,'previous');
C(isnan(C))=B(isnan(C));
[Lia,LocB]=ismember(B,A);
idx=LocB(Lia);
C(Lia)=A(idx)-A(idx-1);
Thanks a lot for the answers! The first value will also exist. So I'll better to alter my previous codes to suit your first answer .
So if the B element value is 5, what should be the expected output value?
Hems
2016년 8월 30일
same as 5 not to be modified.
Hems
2016년 8월 30일
I have another question similar to that if A=[5 10 15 20 22 24 26 30 34] in A first 4 element increased by 5 till 20 and next element from 20 to 26 increased by 2 and the rest is increased by 4 when only the incremants changes compare to previous I need to recalculate it by making it orgin. B=[1 1.5 2 4 5 8 14 17 25 18 16 22 23 20 27 29 32 34]; and the recalculation has to be happened only during the differnce between the element changes at A matrix. answer C=[1 1.5 2 4 5 8 14 17 3 18 16 2 3 17 1 20 1 2 6 8]. could you help me to fix this. Thanks!
It's hard for me to understand the logic based on your description. Matrix operation has its limit. You can't expect to have everything done in matrix operation without a for-loop.
Here is the generic code for your original question, taking into consideration that the element value in B could match the first element value in A. Note the value 5, 20 and 25 in this example. The increment in A can be any value and varying. It doesn't have to be a constant value.
A=[5 10 15 20 25];
B=[1 1.5 2 4 8 14 17 20 5 25 18 16 22 23 17];
C=interp1(A,A,B,'linear')-interp1(A,A,B,'previous');
C(isnan(C))=B(isnan(C));
A_New=[0,A];
[Lia,LocB]=ismember(B,A_New);
idx=LocB(Lia);
C(Lia)=A_New(idx)-A_New(idx-1);
Hems
2016년 8월 30일
Thanks a lot!
my question was i am trying to do the same operation but not at every instant, only when the incremental value changes between A matrix element.
Hems
2016년 8월 31일
A=[ 2 4 6 10 14 18 21 24 27 30] this matrix has constant interval till particular elementts then changes A(1,1)=2 to A(1,3)=6 incremented by 2 A(1,3)=6 to A(1,6)=18 incremented by 4 from previous elements A(1,6) =18to A(1,10)=30 incremented by 3 from previous elements B=[1 3 2 4 5 8 14 17 25 18 16 22 23 20 27 29]; random numbers between 0 to 30. I want, when the increment changes, it has to be fixed as origin and recalculated for ‘C’ matrix In A matrix till number 6 no change in increment so C=[1 3 2 4 5 But 6 to 18 it changed to 4 so from 8 onwards B consider 6 as origin and find the difference so C=[1 3 2 4 5 2 8 11 But again at B (1,9)=25 fall under “A (1,6)=18 to A(1,10)=30 incremented by 3 this starts from A(1,6)=18 it has to be origin for that so in B it is replaced by 25-18=7 And next one B(1,10)=18 where increment changes in A(1,6) makes C(1,10)=0 So finally C=[1 3 2 4 5 8 8 11 7 0 6 4 5 2 9 12] I hope this will explain more clearly about what i am trying to do.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
