Comapre matrix and function a operation

1 visualización (últimos 30 días)
Hems
Hems el 29 de Ag. de 2016
Comentada: Hems el 31 de Ag. de 2016
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!

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 29 de Ag. de 2016
Editada: Fangjun Jiang el 29 de Ag. de 2016
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 comentarios
Hems
Hems el 30 de Ag. de 2016
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
Hems el 31 de Ag. de 2016
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MRI en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by