How can I equate all values of one vector to another and get the value ? Please find the example below

2 ビュー (過去 30 日間)
If I have
dataA = [1,2,3,4,5,6,7,8,9];
dataB = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1];
I need to equate for example: 1 = 0.1, 2=0.2, 3=0.3 so that if I ask value(7) it should display 0.7
Thank you in Advance.
  3 件のコメント
Rakesh Yadav Kodari
Rakesh Yadav Kodari 2019 年 2 月 18 日
May be I asked question the other way I think it would be better by real values
If dataA = -496,-636,-52,175,-84,-248,-555,-423,34,-174
dataB = -0.399609375000000,-0.512402343750000,-0.0418945312500000,0.140991210937500,-0.0676757812500000,-0.199804687500000,-0.447143554687500,-0.340795898437500,0.0273925781250000,-0.140185546875000
Now here How can i equate the the value at -496 = -0.399609375000000 ? %the first value of A is equal to first value of B
Rik
Rik 2019 年 2 月 18 日
If your data is already paired, what exactly do you want to do? Mathematically it doesn't really make sense to equate them, as they are not equal. What do you want to use this for? Maybe then we can help you solve that problem instead.

サインインしてコメントする。

採用された回答

Andrei Bobrov
Andrei Bobrov 2019 年 2 月 18 日
編集済み: Andrei Bobrov 2019 年 2 月 18 日
dataA = [-496,-636,-52,175,-84,-248,-555,-423,34,-174]';
dataB = [-0.399609375000000,-0.512402343750000,-0.0418945312500000,...
0.140991210937500,-0.0676757812500000,-0.199804687500000,...
-0.447143554687500,-0.340795898437500,0.0273925781250000,-0.140185546875000]';
x = [-636,-84,34];
[lo,ii] = ismember(x,dataA);
out = dataB(ii(lo));
  3 件のコメント
Stephen23
Stephen23 2019 年 2 月 18 日
編集済み: Stephen23 2019 年 2 月 18 日
@madhan ravi: you are right, but only if all elements of x are members of dataA. Robust programming would not assume this, which is probably why Andrei Bobrov prefers ii(lo), thus allowing for non-member values without causing an error.
If a non-member value is unacceptable to the algorithm then it would still be better to use assert and write a specific error message, rather than just rely on the uninformative "Indices must be positive integers" that would occur otherwise.
madhan ravi
madhan ravi 2019 年 2 月 18 日
@Stephen Cobeldick: +1 really a good explanation , thank you!

サインインしてコメントする。

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2019 年 2 月 18 日
You can use logical indexing, a very powerful tool in Matlab
tf = dataB == 7
dataA(tf)
  4 件のコメント
Stephen23
Stephen23 2019 年 2 月 18 日
編集済み: Image Analyst 2019 年 2 月 18 日
@Jos: I guess that should be with double equals:
dataB(dataA == -496)
Jos (10584)
Jos (10584) 2019 年 2 月 18 日
yes, of course. Thanks for the correction, Stephen :-)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by