Interpolating between columns for an index

2 次查看(过去 30 天)
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.

采纳的回答

Voss
Voss 2024-8-19
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))
factor = 5x1
1.0000 1.1500 1.5000 1.3650 5.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
result = data(:,2).*factor
result = 5x1
22.0000 24.1500 30.0000 38.2200 70.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

更多回答(1 个)

dpb
dpb 2024-8-19
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...
V = 5x1
22.0000 24.1500 30.0000 38.2200 70.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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 个评论
Eric
Eric 2024-8-19
I will try this method also.
I was going to add one more variable to both the lookup and the data. I don't think that is possible? I would just have to write out seperate factors?
550 750 950
1 2 8
6 4 2
550 22 .2
580 21 -2.5
650 20 0
623 28 .25
850 14 25
I would still be referencing the data on column 1 to row 1 of the lookup table. I would just be almost multiplying column 3 of the data against row 3 of the lookup table.
dpb
dpb 2024-8-20
"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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Lookup Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by