How to map one array elements to another array elements?

56 ビュー (過去 30 日間)
Struggling in MATLAB
Struggling in MATLAB 2022 年 7 月 29 日
編集済み: dpb 2022 年 7 月 29 日
I have an output array which shows to which feeder the animal goes in each trial for 6 trials. There could be only 4 feeders(1,2,3,4)
feederNum = [1 2 2 4 3 1]
The corresponding concentration of reward at the feeders are
feeder1 : 40, feeder2 : 30, feeder3: 20, feeder4: 10
I want to get the corresponding concentraions for the feederNum array. So my desired output is
conc = [40 30 30 10 20 40]
I am wondering if there is any key-value like feature to achieve this?

採用された回答

Stephen23
Stephen23 2022 年 7 月 29 日
編集済み: Stephen23 2022 年 7 月 29 日
Method one: indexing:
num = [1,2,2,4,3,1];
val = [40,30,20,10];
out = val(num)
out = 1×6
40 30 30 10 20 40
Method two: interpolation:
out = interp1(val,num)
out = 1×6
40 30 30 10 20 40

その他の回答 (1 件)

dpb
dpb 2022 年 7 月 29 日
編集済み: dpb 2022 年 7 月 29 日
>> awardCoef=[-10,50];
>> conc=polyval(awardCoef,feederNum)
conc =
40 30 30 10 20 40
>>
More generically if isn't a polynomial (linear in this case) relationship, use lookup tables...
award=40:-10:10;
conc=award(feederNum);
Here award can be anything for each; just has to have a 1:1 entry to the number of values in feederNum -- and, of course the values of feederNum are from 1:numel(feederNum) since MATLAB arrays are one-based indexing.
If must change the numbering system to not be 1:N, then interp1 with the 'nearest' option can be used as the lookup table; same identical idea but not a direct array indexing operation.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by