Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
faster sqi-squared calculation needed

Subject: faster sqi-squared calculation needed

From: Eric

Date: 23 Mar, 2011 14:49:05

Message: 1 of 3

Is there any way to make the codes below faster? mat is a feature by record matrix, gamma is returned as the result of one over average sqi-squared distance. The matrix is big, 4k x 10k. Thanks in advance.

function gamma = chisq(mat)

allcol = size(mat, 2);
total = 0;
for col =1:allcol-1
        D = (repmat(mat(:,col),1,allcol-col)-mat(:,col+1:end)).^2./(repmat(mat(:,col),1,allcol-col)+mat(:,col+1:end));
        D(isnan(D)) = 0;
        total = total + sum(sum(D));
end

gamma = (2*allcol-3) / total;

Subject: faster sqi-squared calculation needed

From: Roger Stafford

Date: 23 Mar, 2011 17:38:05

Message: 2 of 3

"Eric" wrote in message <imd190$arp$1@fred.mathworks.com>...
> Is there any way to make the codes below faster? mat is a feature by record matrix, gamma is returned as the result of one over average sqi-squared distance. The matrix is big, 4k x 10k. Thanks in advance.
>
> function gamma = chisq(mat)
>
> allcol = size(mat, 2);
> total = 0;
> for col =1:allcol-1
> D = (repmat(mat(:,col),1,allcol-col)-mat(:,col+1:end)).^2./(repmat(mat(:,col),1,allcol-col)+mat(:,col+1:end));
> D(isnan(D)) = 0;
> total = total + sum(sum(D));
> end
>
> gamma = (2*allcol-3) / total;
- - - - - - - - -
  Have you tried nested for-loops for this? Perhaps the for-loop overhead is small compared with the overhead of forming those huge repmats.

a = size(mat,2);
t = 0;
for c1 = 1:a-1
 p = mat(:,c1);
 for c2 = c1+1:a
  q = mat(:,c2);
  d = (p-q).^2./(p+q);
  t = t + sum(d(~isnan));
 end
end

Roger Stafford

Subject: faster sqi-squared calculation needed

From: Eric

Date: 25 Mar, 2011 11:55:20

Message: 3 of 3

I doubted at first. However your code is confirmed faster after testing. I am puzzled, I thought matlab runs faster without using loop, especially nested loop. Why repmat requires more processing time?

"Roger Stafford" wrote in message <imdb5t$cgf$1@fred.mathworks.com>...
> "Eric" wrote in message <imd190$arp$1@fred.mathworks.com>...
> > Is there any way to make the codes below faster? mat is a feature by record matrix, gamma is returned as the result of one over average sqi-squared distance. The matrix is big, 4k x 10k. Thanks in advance.
> >
> > function gamma = chisq(mat)
> >
> > allcol = size(mat, 2);
> > total = 0;
> > for col =1:allcol-1
> > D = (repmat(mat(:,col),1,allcol-col)-mat(:,col+1:end)).^2./(repmat(mat(:,col),1,allcol-col)+mat(:,col+1:end));
> > D(isnan(D)) = 0;
> > total = total + sum(sum(D));
> > end
> >
> > gamma = (2*allcol-3) / total;
> - - - - - - - - -
> Have you tried nested for-loops for this? Perhaps the for-loop overhead is small compared with the overhead of forming those huge repmats.
>
> a = size(mat,2);
> t = 0;
> for c1 = 1:a-1
> p = mat(:,c1);
> for c2 = c1+1:a
> q = mat(:,c2);
> d = (p-q).^2./(p+q);
> t = t + sum(d(~isnan));
> end
> end
>
> Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us