# Thread Subject: Vectorizing Nested For Loops for mscohere

 Subject: Vectorizing Nested For Loops for mscohere From: Samuel Finlayson Date: 12 Jul, 2012 17:56:13 Message: 1 of 2 Hello, I am trying for the first time to use matlab to calculate coherence between every possible pair of sensors across a lot of timepoints. I need to compare each pair at a number of windows of data. I am running into efficiency issues. I am assume this is a simple vectorization problem, but I haven't done vectorization before and don't want to mess it up, and would thus appreciate your help in at least learning the principles at play. My first step in optimizing was to make the second loop go from i:n, because coherence is symmetric. This cut down half of the calculations, but it is still very very slow. data_coh = cell(nwindows, n,n); data_fq = cell(nwindows, n, n); for i = 1: n;     for j = i: n;         for k = 1:nwindows;            [gcoh,gfqr] = mscohere(datapoint(i).win(k).time, datapoint(j).win(k).time);            data_coh{k,i,j} = gcoh;            data_coh{k,j,i} = gcoh;            data_fq{k,i,j} = gfqr;            data_fq{k,j,i} = gfqr;         end     end end Thanks so much!
 Subject: Vectorizing Nested For Loops for mscohere From: Sam Date: 12 Jul, 2012 18:14:17 Message: 2 of 2 Sorry, reposted to "answers," my mistake. "Sam" wrote in message ... > Hello, > > I am trying for the first time to use matlab to calculate coherence between every possible pair of sensors across a lot of timepoints. I need to compare each pair at a number of windows of data. I am running into efficiency issues. I am assume this is a simple vectorization problem, but I haven't done vectorization before and don't want to mess it up, and would thus appreciate your help in at least learning the principles at play. > > My first step in optimizing was to make the second loop go from i:n, because coherence is symmetric. This cut down half of the calculations, but it is still very very slow. > > data_coh = cell(nwindows, n,n); > data_fq = cell(nwindows, n, n); > > for i = 1: n; > for j = i: n; > for k = 1:nwindows; > [gcoh,gfqr] = mscohere(datapoint(i).win(k).time, datapoint(j).win(k).time); > data_coh{k,i,j} = gcoh; > data_coh{k,j,i} = gcoh; > data_fq{k,i,j} = gfqr; > data_fq{k,j,i} = gfqr; > end > end > end > > Thanks so much!

### Everyone's Tags:

Separated by commas
Ex.: root locus, bode

### 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.