can this loop be vectorized
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Guillaume
2019년 3월 1일
c = a ~= a.' %works since R2016b
prior to R2016:
c = bsxfun(@ne, a, a.') %ne is the function name of ~=
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Special Functions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!