Interpolating between columns for an index
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Voss
on 19 Aug 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 Comments
More Answers (1)
dpb
on 19 Aug 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 Comments
dpb
on 20 Aug 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.
See Also
Categories
Find more on Lookup Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!