Comapre matrix and function a operation
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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 .
Fangjun Jiang
2016 年 8 月 30 日
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!
Fangjun Jiang
2016 年 8 月 30 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
