File Exchange

image thumbnail

Cooccurrence matrix

version (1.7 KB) by Baran Aydogan
Calculates cooccurrence matrix for a given direction and distance


Updated 25 Jul 2006

View License

Calculates cooccurrence matrix of gray scale images for a given direction (0, +45, +90, +135, +180, -135, -90, -45) and distance

Cite As

Baran Aydogan (2021). Cooccurrence matrix (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (15)


komal latif

in which language u run co-occurrence matrix code? @baran Ayodgan

komal latif

if anyone have co-occurrence matrix algorithm. plz email me i need

france k

Dear friend can any one explain the for loop (intensity1,intensity2) how it works.

Abhishek Singh

I am using this code and it is very slow. Can anyone provide the vectorization version of this code or vectorization version of co-occurrence matrix.

abs pir



hu defeng

John Bertrand

This looks similar to c = n12/(n1 + n2 - n12)

Kartik Gupta

engin tola

to ludvik: thanks for the warning. I didn't know that.

Ludvik T

Comment to engin tola: no, you are wrong. It is evaluated only once before all iterations before the "for" cycle, so your proposal would make it slower. Try this:

a = zeros(5,1);
for f = 1:size(a,1);
a = zeros(10,1);

In fact the "for" cycle is equivalent to "cycle through vector" command. This slightly explains how the "for" cycle really works in Matlab:

for f = [2,1:3,700,ones(1,5),pi,7.564];

engin tola

instead of using i = 1:size(ind_x1,1) kind of indexing for "for loops" you should compute x1sz=size(ind_x1,1) once and use i = 1:x1sz so that code will not call "size" function for every iteration needlessly.

another point to speed-up the code is to break the inner for loop where the "count" variable is incremented since once the match of indexes is found, it is not necessary to continue searching for the match.

anu gawade


Dear Baran Aydogan
I was perused your program code. Your program has some line`s block if-end codes. This region if this code application switch(parameter)-case{} codes than more small. For example your code;

if (dir == 0)
dir_x = 0;
dir_y = 1;

if (dir == 1)
dir_x = 1;
dir_y = 1;

if (dir == 2)
dir_x = 1;
dir_y = 0;

%####I adive codes####
% |
%_______for i=0 than
dir_x = 0;
dir_y = 1;
%_______for i=1 than
dir_x = 1;
dir_y = 1;
%_______for i=2 than
dir_x = 1;
dir_y = 0;


good lock...

MATLAB Release Compatibility
Created with R14SP3
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!