 # How do you subtract 1st dim all values in a 3D matrix with 1st dim all values of another 3D matrix?

Hi Mike,
Thank you for all the great videos! I’m trying your suggested exercise from “Complete Neural Signal Processing and Analysis: Zero to Hero” 185: Phase synchronization matrices in multitrial
data at about 12:30 where you suggest to take away the 2 for loops and use bsxfun instead. I’m having quite a bit of trouble with it and was wondering if you may have more input on the matter.

I have two 3D matrices that are the same.
size(A) = [10 100 50]
size(B) = [10 100 50]

I want to subtract along every value in the 1st dim of A with every value in 1st dim of B with no for loops so using bsxfun. I will also take the mean in the middle

I want to end up with 10x10x50. How do I do this?

So far, I just have

z = mean(bsxfun(@minus, A, B, 2)

which subtracts every matching data index in 1st dim so [1-1] [2-2] … [10-10]
I’m left with a 10x1x50 matrix of zeros.
I want to subtract along the 1st dim of A and B
| [1-1] [1-2] … [1-10]
| [2-1] …
| …
V [10-1] [10-2] … [10-10]
Matrix A…Matrix B - - ->

I can do this for loop and I get the 10x10x50 matrix I want:
for i = 1:10
for j = 1:10
z = mean(bsxfun(@minus, A(i), B(j), 2)
end
end

How would you do this without for loops?

Hi Ryan. I’m a bit confused about your data, perhaps you can clarify. What are the dimensions of A and B? And why would you want to end up with 10x10x50? It’s not clear to me why or how the second dimension would go from 100 to 10. If you just want to subtract the matrices for the first 10 elements of the second dimension, then you don’t even need bsxfun:
`Z = A(:,1:10,:) - B(:,1:10,:);`

Dim A is a 3D matrix of [Channels x Time Points x Trials]
Dim B is the same matrix as A.
They are basically tmpAi and tmpAj from “Complete Neural Signal Processing and Analysis: Zero to Hero - 185: Phase synchronization matrices in multi-trial data"

I want to take the channels and do phase-locking value against every other channel in single trials without 2 for loops. This matrix Z will be a matrix of [10 Channels x 10 channels x 50 trials] so essentially do phase locking value comparing each channel with other channels and take the mean of the time points in each individual trial. So you have 10x10 phase locking values for each trial.

so Z( 1 ,1 , : ) gives phase synchronization of channel 1 and channel 1 for all 50 trials
Z( 1 ,2 , : ) gives phase synchronization of channel 1 and channel 2 for all 50 trials

Z( 10 ,9 , : ) gives phase synchronization of channel 10 and channel 9 for all 50 trials

I am still stuck on this problem and wondering if bsxfun or another function can be used to take out the for loops

What I have right now is
for i = 1:10
for j = 1:10
z = mean(bsxfun(@minus, A (i), B (j), 2)
end
end

You suggested a challenge at around 12:30 on video 185 of Neural Signal Prcessing and Analysis Zero to Hero to take out the for loops and I’m still having difficulties with it. Should I be using the bsxfun several times? Is there another function that helps with this?

OK, got it, thanks for clarifying. I have a hint for you below. In the code, C1 and C2 are identical. Notice that I didn’t use bsxfun here; newer versions of MATLAB will do binary expansion automatically. If the `C1(i,:,:) =` line gives you an error, you can use bsxfun there. I hope that helps get you going!

``````A = randn(10,300,50);
B = randn(10,300,50);
[C1,C2] = deal( zeros(10,10,50) );

for i=1:10
C1(i,:,:) = mean(A(i,:,:) - B,2);
for j=1:10
C2(i,j,:) = mean( A(i,:,:) - B(j,:,:) ,2);
end
end``````