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 个评论
If it is 25 then it should be 25-20=5 and for 20 20-15=5
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?
same as 5 not to be modified.
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);
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.
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)
