sumnot3([1 2 3; 4 5 6; 7 8 9]) % correct answer 1+2+4+5+6+7+8+9=42
function mod_sum = sumnot3(A)
[rows cols] = size(A);
Avec = A(:);
mod_sum = sum(Avec);
% code to add to mod_sum entries of A not equal to 3
end
how to get the sum all the elements inside matrix A, excluding 3?

1 件のコメント

Arif Hoq
Arif Hoq 2022 年 1 月 28 日
You can try with this:
function mod_sum = sumnot3(A)
[rows cols] = size(A);
A3=A(1,end);
rep=A3==3;
A(rep,end)=0;
mod_sum = sum(A,'all');
% code to add to mod_sum entries of A not equal to 3
end

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

 採用された回答

DGM
DGM 2022 年 1 月 28 日

0 投票

It's unclear whether you want to exclude A(1,3) or whether you want to exclude the number 3 regardless of its location.
A = [1 2 3; 4 5 6; 7 8 9];
B1 = sum(A(:)) - A(1,3)
B1 = 42
B2 = sum(A(A~=3)) % works for integers
B2 = 42

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by