Assigning values to vectors of different lenghts

1 回表示 (過去 30 日間)
Christina
Christina 2015 年 3 月 27 日
回答済み: Jon 2015 年 3 月 27 日
Hello,
I've got three vectors, say a(1x4), b(1x4) and c(1x10). Vectors a and c represent the same thing, they are just codes. Vector b let's say it's my objective function.
a = [1 2 3 4]
b = [0.1 0.2 0.3 0.4]
c= [ 4 4 4 2 2 1 3 3 1 4].
I want to get another vector d, which will have the same dimensions as vector d (10x1) and in which the values of vector b will be assigned.
d should be like d = [0.4 0.4 0.4 0.2 0.2 0.1 0.3 0.3 0.1 0.4].
Any thoughts on this?
Many thanks

採用された回答

Konstantinos Sofos
Konstantinos Sofos 2015 年 3 月 27 日
編集済み: Konstantinos Sofos 2015 年 3 月 27 日
Hi,
if your starting point is that you have a and c you can make d from b as
[~,idx] = ismember(c,a);
d = b(idx);
>> d
d =
0.4000 0.4000 0.4000 0.2000 0.2000 0.1000 0.3000 0.3000 0.1000 0.4000
  1 件のコメント
Christina
Christina 2015 年 3 月 27 日
Good, it works!
Many thanks!

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

その他の回答 (1 件)

Jon
Jon 2015 年 3 月 27 日
It's not clear what you need a for. You can make d directly from c and b as follows:
>> d=b(c)
d =
0.4 0.4 0.4 0.2 0.2 0.1
0.3 0.3 0.1 0.4

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by