combining matrices by replacing zeros

I have two matrices of the same length;
A = [1 ,0, 2, 0, 3, 0, 5]; B = [0, 2, 0, 3, 0, 5, 0];
I want to be 'combine' them into one matrix where the zeros 'disappear'; C = [1, 2, 2, 3, 3, 5, 5];
May I know how to do this?

 採用された回答

Jan
Jan 2018 年 1 月 8 日
編集済み: Jan 2018 年 1 月 8 日

0 投票

A = [1 ,0, 2, 0, 3, 0, 5];
B = [0, 2, 0, 3, 0, 5, 0];
C = A + B
Or if the zeros need not be at the same indices as the non-zeros in the other variable:
C = A;
C(A == 0) = B(B ~= 0)

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

タグ

質問済み:

cgo
2018 年 1 月 8 日

編集済み:

Jan
2018 年 1 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by