apply if else condition on 3d array

Hi,
I want to use a simple if else statement to for three matrices A, B, and C of equal dimenstion (11x12x32). Matrix C=zeros(11,12,32) I'm trying to get the following result: If an elements value in matrix A=0 then the respective element in C should be copied from the respective element in matrix B. If not the elemement in matrix C should remain 0.
I tried wo work with the following code. Yet it doesn't work: C=zeros(11,12,32);
if A==0 C=B; else C=0; end
Any advice? Thank you so much in advance.

回答 (2 件)

Mischa Kim
Mischa Kim 2014 年 3 月 14 日
編集済み: Mischa Kim 2014 年 3 月 14 日

0 投票

Christoph, use
C = bsxfun(@times,B,A==0)
Andrei Bobrov
Andrei Bobrov 2014 年 3 月 14 日

0 投票

l = A == 0;
C(t) = B(t);

カテゴリ

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

タグ

質問済み:

2014 年 3 月 14 日

回答済み:

2014 年 3 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by