Subtract column vectors from eachother in every possible constelation

I have a column matrix "Sample (10,n)" and now I want to subtract the column matrix "Testp (10,n)" from the Sample-matrix. To be more specific, I need to subtract the columns from eachother so that "(Sample(:,1) - TestP(:,1)) + (Sample(:,2) - TestP(:,2)) + ... " will be executed. But it has to be done for every constelation so that for n-th column every entry is taken, than the n-th-1 column switches to the second row-entry and from the n-th vector every value is taken and so on, that in the end I will get a column matrix "gamma (10,don't know".

4 commentaires

Jan
Jan le 13 Nov 2017
Modifié(e) : Jan le 13 Nov 2017
What is a "column matrix" and which size does "Sample(10, n)" have? If n = 2, what is the size of the output? "don't know" is a little vague to explain clearly, what you want to achieve. Is it n^2?
The column matrix is just a matrix build together witch colums. So column 1 is, lets say the observed values from test 1, second column the observed values from test 2 an so on.
The size of "Sample" is 50x10.
I wrote the "don't know" because I didn't know the exact number of possible combinations but for a 50x10 matrix
Maybe this example could help it's a bit hard to explain.
Sample = [a1 b1 c1; a2 b2 c2; a3 b3 c3]
TestP = [x1 y1 z1; x2 y2 z2; x3 y3 z3]
And what I want to achieve is a matrix that looks like this:
C = |(a1-x1)+(b1-y1)+(c1-z1) (a1-x1)+(b1-y2)+(c1-z1) (a1-x1)+(b1-y3)+(c1-z1) ...|
|(a1-x1)+(b1-y1)+(c1-z2) (a1-x1)+(b1-y2)+(c1-z2) (a1-x1)+(b1-y3)+(c1-z2) ...|
|(a1-x1)+(b1-y1)+(c1-z3) (a1-x1)+(b1-y2)+(c1-z3) (a1-x1)+(b1-y3)+(c1-z3) ...|
Hope it helps. I need a generalized command for that, so I can use it for nxm matrices.
So, does your C also include (b2-y1), (b2-y2), ..., (b3-y1), ... and for the 1st column (a1-x2), ... (a2-x1), ... ?
If so there shouldn't be any I don't know, the number of combinations is size(Sample, 1) ^ (2* size(Sample, 2))
For a 50x10 matrix, that is about 9.7e16 elements something that would require over 710,000 petabytes of memory. I doubt you have anywhere that amount of memory (or the time required to calculate that many elements), so you may want to rethink your problem.
I assumed it would be a large number of combinations, but wow 9.7e16 elements?! I clearly have to rethink this :P
But thank you for your quick help!

Connectez-vous pour commenter.

Réponses (1)

With some guessing:
n = 7;
Sample = rand(10, n);
Testp = rand(10, n);
gamma = Sample - reshape(Testp, 10, 1, n); % >= R2016b: Auto-Expand
With older Matlab versions:
gamma = bsxfun(@minus, Sample, reshape(Testp, 10, 1, n));
Is this what you want? Perhaps a:
gamma = reshape(gamma, 10, []);

Question posée :

le 13 Nov 2017

Commenté :

le 13 Nov 2017

Community Treasure Hunt

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

Start Hunting!

Translated by