Combining columns of two matrices

조회 수: 3 (최근 30일)
Joel Schelander
Joel Schelander 2021년 3월 18일
댓글: Joel Schelander 2021년 3월 19일
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

채택된 답변

Jan
Jan 2021년 3월 18일
편집: Jan 2021년 3월 18일
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
Walter Roberson
Walter Roberson 2021년 3월 19일
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.
Joel Schelander
Joel Schelander 2021년 3월 19일
Thank you

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by