Join elements of an array

1 次查看(过去 30 天)
fede
fede 2015-7-12
If I have two array
a=[1 2 3 0]
b=[0 0 5 4]
and I want the array c=[1 2 3 4] How should I proceed?
In other words I want a array c in which the element of array a==0 is replaced with the element of matrix b.

回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2015-7-12
编辑:Azzi Abdelmalek 2015-7-12
a=[1 2 3 0]
b=[0 0 5 4]
idx=a==0
bb=sort(nonzeros(b))
a(idx)=bb(1:sum(idx))
  2 个评论
fede
fede 2015-7-12
编辑:Azzi Abdelmalek 2015-7-12
mmmm but if i want a general formula? I have two arrays of prices.
a=[p1 p2 p3 0 0 0 ....]
b=[0 0 p3 p4 p5 0 0....]
and I want join the two arrays to obtain a array c in which I have
c=[p1 p2 p3 p4 p5]
Azzi Abdelmalek
Azzi Abdelmalek 2015-7-12
Have you tried my code?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by