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 Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by