Combining columns of two matrices

I have 8 Battery electric vehicles and 16 apartments.
The 8 Battery electric vehicles are stored in a 525600*8 matrix (A)
The 16 apartments are gathered in one matrix (525600x16) (Appartment),.
Both are describing a power demand.
I want to combine these, i.e. Add up the power demands. This is how far I got, but I dont know how I do for all combinations.
x = randi(size(Appartment,2));
App = Appartment(:,x);
y = randi(size(A,2));
Vehicle = A(:,y);
P=App(:,1)+Vehicle(:,1)';
I=max(P)/max(App(:,1));
I is how much the max power demand increases when a Battery electric vehicle is introduced.
I want to save the I values for all combinations for later use

 Accepted Answer

Jan
Jan on 18 Mar 2021
Edited: Jan on 18 Mar 2021
If you want to find all combinations of 2 values, simply use two loops:
for x = 1:size(Appartment, 2)
App = Appartment(:,x);
for y = 1:size(A, 2)
Vehicle = A(:,y);
P = App(:,1) + Vehicle(:,1)';
I = max(P) / max(App(:,1));
end
end
I guess, you want to store the valuzes of I? Then you need something like I(x,y) = ... .
There are faster versions without loops also. But lets clarify at first, if this is, what you are searching for.

7 Comments

Yes exactly I want to store the values if I. I would also like ti be able to trace back to the apartment/vehicle somehow. And yes, a faster version would be nice..
I(x,y) = max(P) / max(App(:,1));
Rows of I will be all the same Appartment column; columns of I will be the same vehicle column.
Okay. If my code does, what you want, this is a simplified version:
nAppartment = size(Appartment, 2);
nA = size(A, 2);
I = zeros(nAppartment, nA);
for x = 1:nAppartment
App = Appartment(:, x);
for y = 1:nA
Vehicle = A(:, y);
P = App + Vehicle.';
I(x, y) = max(P) / max(App);
end
end
Althought this can be vectorized, the size of the input arrays with 525600 elements does not allow to do this: you would need an array with 525600 * 525600 * 8 * 16 and 8 bytes per elements, which would need 283 TB of RAM.
Even your code needs huge intermediate matrices for P of the size 24.7 GB, which does exhaust my computer.
Yes, I got this error:
Requested 525600x525600 (2058.3GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become
unresponsive. See array size limit or preference panel for more information.
Error in RELATION (line 35)
P = App + Vehicle.';
I guess this means that theres no error, just simply not enough capacity?
I have the option to do the calculations on the Universitys computer, so will try that
However if I do this
nAppartment = 1 %size(Appartment, 2);
nA = 1 %size(A, 2);
I = zeros(nAppartment, nA);
for x = 1:nAppartment
App = Appartment(:, x);
for y = 1:nA
Vehicle = A(:, y);
P = App + Vehicle.';
I(x, y) = max(P) / max(App);
end
end
I still get the error
Requested 525600x525600 (2058.3GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become
unresponsive. See array size limit or preference panel for more information.
Error in RELATION (line 35)
P = App + Vehicle.';
Here I will be only one value, which I managed with my own code.
App = Appartment(:, x);
That's a column vector.
Vehicle = A(:, y);
That's a column vector.
P = App + Vehicle.';
The .' turns the column vector Vehicle into a row vector. Then you add each element of the row vector to each element of the column vector, getting a huge 2D addition table.
I(x, y) = max(P) / max(App);
P is a huge 2D addition table. You are taking max() along each column. But you can predict the result: it is going to be max() along the column vector App plus the current value of the (switched to row) vector Vehicle. So without forming P, you can reduce that down to
nAppartment = 1 %size(Appartment, 2);
nA = 1 %size(A, 2);
I = zeros(nAppartment, nA);
for x = 1:nAppartment
App = Appartment(:, x);
for y = 1:nA
Vehicle = A(:, y);
I(x, y) = (max(App) + Vehicle) / max(App);
end
end
which in turn would be
I(x,y) = 1 + Vehicle.'./max(App);
You might notice this is a row vector; you would not be able to store that in the single location I(x,y) so you would need something like
I(x,y,:) = 1 + Vehicle.'./max(App);
and for efficiency you can skip the 1+ part and add 1 to the entire matrix afterwards, so you could have
I1(x,y,:) = Vehicle.'./max(App)
and with some work you could vectorize the whole thing.
You are getting a 16 x 8 x 525600 matrix as output, which would be just over 1/2 gigabytes.
And remember to add 1 to it afterwards if you need to.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!