フィルターのクリア

can this loop be vectorized

1 回表示 (過去 30 日間)
Bright Edison
Bright Edison 2019 年 3 月 1 日
コメント済み: Bright Edison 2019 年 3 月 1 日
Can this loop be vectorized?
a=[5,7,8,5,6];
n=length(a);
b=a;
c=zeros(n,n);
for i=1:length(a)
for j=1:length(a)
if a(i)==b(j)
c(i,j)=0;
end
if a(i)~=b(j)
c(i,j)=1;
end
end
end
c
It returns
0 1 1 0 1
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
1 1 1 1 0

採用された回答

Guillaume
Guillaume 2019 年 3 月 1 日
c = a ~= a.' %works since R2016b
prior to R2016:
c = bsxfun(@ne, a, a.') %ne is the function name of ~=
  1 件のコメント
Bright Edison
Bright Edison 2019 年 3 月 1 日
Thanks!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by