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

11 visualizzazioni (ultimi 30 giorni)
Ryan Thackston il 14 Apr 2020
Modificato: Ryan Thackston il 14 Apr 2020
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 do
[1-1] [1-2] ... [1-10]
[2-1] ...
...
[10-1] [10-2] ... [10-10]
I can do this 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
Is this possible without for loops?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (1)

AKHILA GOUDA il 14 Apr 2020
If I understand your question then you simple go through matrix element wise operation
A=rand(10,100,50);
B=rand(10,100,50);
C=A(:,1:10,:)-B(:,1:10,:);
size(C)
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Ryan Thackston il 14 Apr 2020
Modificato: Ryan Thackston il 14 Apr 2020
No that's not it, sorry I fixed some of the wording in my statement. I want to use all data from the 2nd dim before I take the mean.
I want to subtract by each data point in the 1st Dim for A & B

Accedi per commentare.

### Categorie

Scopri di più su Matrix Indexing in Help Center e File Exchange

R2017b

### Community Treasure Hunt

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

Start Hunting!

Translated by