Efficient way to calculate distance matrix between cells

3 views (last 30 days)
Hi,
So i have an image with size of 512x512 pixels
For each pixel i calculte an environment of 11x11 nighbours so now i have matrix of cells in size of 512x512 while each cell contain a matrix in size of 11x11.
i want to calculte in efficient way a distance matrix between the cell's matrix elements, i.e if i organzie the cells marix in colum stack (vector in size of 262144), lets call this vector A , (so A is vector of cells while each cell contain a 11x11 matrix) i would like to create a matrix D in size of 262144x262144 while D(i,j) is the euclidian distance between the matrices in A(i) and A(j),
So far i calculate it with for loops:
function [ D ] = DistanceMatFromPatches( patches )
[patchesH,patchesW]=size(patches);
D=zeros(patchesH,patchesW);
patches_cs=patches(:);
patchesSize=size(patches_cs);
patchesNum=patchesSize(1);
f = waitbar(0);
for i=1:patchesNum-1
first_cs=patches_cs{i}(:);
for j=(i+1):(patchesNum)
waitbar(j/patchesNum,f,sprintf('Calculate Distance matrix: pixels %d and %d from %d',i,j,patchesNum));
%pause(0.1);
sec_cs=patches_cs{j}(:);
D(i,j)=norm(first_cs-sec_cs);
D(j,i)=D(i,j);
end
end
close(f);
end
while patches is 512x512 matrix of cells.
This implementation is not very efficent .. anyone has other idea?

Answers (1)

Jan
Jan on 28 Jan 2021
Edited: Jan on 28 Jan 2021
A 262144 x 262144 matrix of type double needs 550 GB of RAM. I assume that this will exhaust your machine.
You need a different approach, which can run on existing computers.
Blowing up the matrix to a cell matrix, which contains 11x11 submatrices does not add any new information to the data. Then this produced redundancy is a waste of ressources already.
  1 Comment
Orr Streicher
Orr Streicher on 28 Jan 2021
Hi Jan and thanks for your response
Any seggestion how to do it?
In the end i would like to find the distance matrix as i described .. maybe there is an efficient way to do it?

Sign in to comment.

Categories

Find more on Dialog Boxes in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!