Interpolating between columns for an index
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I am trying to use a seperate array as an index and a variable.
E.g.
Row 1 I want to use as index, or lookup.
Row 2 are data
550 750 950
1 2 8
Column 1 would be reference from row 1
Column 2 would be multiplied against row 2 depending on how Column 1 relates to row 1
550 22
580 21
650 20
623 28
850 14
So my goal is using the second set of data.
at 550, 22 would be multiplied by 1,
at 650, 20 would be multiplied by 1.5
at 850, 14 would be multiplied by 5
I tried search for awhile on the community and through the "basics"
I might be wording this wrong by calling it indexing.
0 comentarios
Respuesta aceptada
Voss
el 19 de Ag. de 2024
lookup = [ ...
550 750 950; ...
1 2 8; ...
];
data = [ ...
550 22; ...
580 21; ...
650 20; ...
623 28; ...
850 14; ...
];
factor = interp1(lookup(1,:),lookup(2,:),data(:,1))
result = data(:,2).*factor
2 comentarios
Más respuestas (1)
dpb
el 19 de Ag. de 2024
X=[550 750 950]; Y=[1 2 8]; % the interpolation table data
xy=[550 22;580 21;650 20;623 28;850 14]; % the data for interpolating, multiplying
V=interp1(X,Y,xy(:,1)).*xy(:,2) % the values...
You could encase the above logic in a function with either input data variable for the table and the lookups or with a fixed table (either stored directly or read from an external file, etc., ...)
2 comentarios
dpb
el 20 de Ag. de 2024
"I will try this method also."
It's identical to @Voss's with the exception of separating the data from the code and using the implicit multiply without returning the multiplier explicitly as separate variable.
The extension would change adding only using the other row/column indices 3 instead of 2 above. Why I suggested wrapping into a function; you could pass the index as well for separate calls if the use case is one or the other, depending on code logic. Or, if it always occurs together, then just add the second line in the function, too.
Ver también
Categorías
Más información sobre Lookup Tables 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!