Replace an element in a vector with another element.

I want to replace the elements of a vector with an element from other vector. But the real glitch here is that i just want 4 values from that vector to be activated and replaced.
So the code goes like this:
g=[1 2 3 4 5 6 7 8 9];
u=[1 1 3 5 4 3 7 8 9];
gout =g;
active_g = [2 3 5 6];
active_u = [2 3 5 6];
U = (g(active_g)>u(active_u))
gout(U) = u(U);
gout(~U) = g(~U);
Now the output should be gout = [1 1 3 4 4 3 7 8 9] But due to U becoming a 1*4 vector.. its not updating elements after that. Kindly suggest a way around. I just need to update the active elements and send it to other function as input but at the same time i need the complete g to evaluate another function.
So its like input g -> function(g,l) -> function (g_updated).

1 件のコメント

James Tursa
James Tursa 2015 年 11 月 11 日
For your example, what is the desired gout?

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

 採用された回答

Mohammad Abouali
Mohammad Abouali 2015 年 11 月 11 日
編集済み: Mohammad Abouali 2015 年 11 月 11 日

0 投票

g=[1 2 3 4 5 6 7 8 9];
u=[1 1 3 5 4 3 7 8 9];
gout=min([g;u])
gout =
       1     1     3     4     4     3     7     8     9

3 件のコメント

Mohammad Abouali
Mohammad Abouali 2015 年 11 月 11 日
another approach is this:
g=[1 2 3 4 5 6 7 8 9];
u=[1 1 3 5 4 3 7 8 9];
activeCells=[2 3 5 6];
mask=false(size(g));
mask(activeCells)=true;
mask= mask & g>u;
gout=g;
gout(mask)=u(mask)
gout =
1 1 3 4 4 3 7 8
kumar vivek
kumar vivek 2015 年 11 月 16 日
Thanks Mohammad Abouali for the answer. It really solved my problem :)
Mohammad Abouali
Mohammad Abouali 2015 年 11 月 16 日
you are welcome

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeAuthor Block Masks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by