필터 지우기
필터 지우기

Another way instead of for loop

조회 수: 2 (최근 30일)
muhammad ismat
muhammad ismat 2015년 6월 18일
편집: Guillaume 2015년 6월 18일
I have this code
for i = 1:34
for j = 1:i % <-- Note the 1:i instead of 1:n
s(i,j) = abs(z(i,ind(j))-z(j,ind(i)))/(z(i,ind(j))+z(j,ind(i)));
s(j,i) = s(i,j)
end
end
firstly, i make a cluster to specific matrix(840000 x 840000) then i want to calculate previous code (with another way instead of for loop) to each point in the matrix
where 'ind' is cluster no that a point belong to, z is the distance from point to a cluster

답변 (1개)

Guillaume
Guillaume 2015년 6월 18일
편집: Guillaume 2015년 6월 18일
The following should work. Basically, use ndgrid (or meshgrid) and sub2ind to compute all your indices at once:
[direct, indirect] = ndgrid(1:34, ind(1:34));
index1 = sub2ind(size(z), direct, indirect));
index2 = sub2ind(size(z), indirect, direct));
s = abs(z(index1) - z(index2)) ./ (z(index1) + z(index2));
I'm calculating the whole matrix at once instead of just the lower triangle as that will be faster anyway than flipping and copying the lower triangle.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by