Clear Filters
Clear Filters

Combining columns of two matrices

2 views (last 30 days)
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
Walter Roberson
Walter Roberson on 19 Mar 2021
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)

Tags

Community Treasure Hunt

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

Start Hunting!