Interpolating between columns for an index
1 回表示 (過去 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.
0 件のコメント
採用された回答
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))
result = data(:,2).*factor
2 件のコメント
その他の回答 (1 件)
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...
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 件のコメント
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 Center および File Exchange で Lookup Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!