If statement / conditional operations

1 回表示 (過去 30 日間)
Chris Brown
Chris Brown 2017 年 2 月 3 日
コメント済み: Chris Brown 2017 年 2 月 3 日
Hi all,
I'm trying to write a code that does something based on the adjacent cells. I'm not sure the best way to do this, but have started using the 'if' statement.
arb is a 3D array, whilst arbgrad is the 1st derivative / gradient of arb.
if ((arb(j,i-1,k)+arb(j,i+1,k)+(arb(j+1,i,k))+arb(j-1,i,k))./4)>(arb(j,i,k));
if arbgrad(j,i,k)<0;
BB1(arbgrad<0)=(arbgrad<0).*1;
else
BB2(arbgrad>0)=(arbgrad>0).*-1;
end
else
if arbgrad(j,i,k)<0
BB3(arbgrad<0)=(arbgrad<0).*1;
else
BB4(arbgrad>0)=(arbgrad>0).*-1;
end
end
arbgrad(j,i,k)=BB1(j,i,k)+BB2(j,i,k)+BB3(j,i,k)+BB4(j,i,k);
The error message keeps coming up:
In an assignment A(:) = B, the number of elements in A and B must be the same.
I'm not sure why because arbgrad, arb and BB1-4 all have the same amount of elements.... Any help would be greatly appreciated.
Thanks!

採用された回答

Adam
Adam 2017 年 2 月 3 日
編集済み: Adam 2017 年 2 月 3 日
(arbgrad<0)
is a logical vector of the same length as arbgrad.
BB3(arbgrad<0)
which you try to assign it to is a vector of length equal to the number of true (1) values in the above vector.
Do you maybe mean to assign:
BB3(arbgrad<0) = arbgrad(arbgrad<0)
?
  1 件のコメント
Chris Brown
Chris Brown 2017 年 2 月 3 日
Yes, thank you. Appreciate your help.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by