Convert 3 dimensional matrix to vector (rows to columns)

Hi I have a 3 dimensional matrix that I am trying to convert the rows to columns and then stack all the columns to generate a vector that will have the three dimensions. I am struggling with the coding of this
So far I have
PV_power_output(:,:,K) = real((Vmpp_(:,:,K).*Impp_(:,:,K))*inverter_efficiency)/1000; % Power output in kW
PV_energy(:,:,K) = sum(sum(PV_power_output(:,:,K))) ; % Energy output in kWh
PV_rows_to_columns(:,:,K) = PV_power_output(:,:,K)';
PV_power_output is a 365x24x3 matrix and I am trying to convert it to a 8760x1x3 vector
Thanks

 採用された回答

Thomas
Thomas 2012 年 6 月 20 日

0 投票

doc reshape
out = reshape(PV_power_output,8760,1,3);
Warning: read the reshape help document thoroughly as it does columnwise reshape..

12 件のコメント

Andrew Alkiviades
Andrew Alkiviades 2012 年 6 月 20 日
this does not seem to convert the row into a column and then stack column 2 under column1, column 3 under column 2 etc to get one column
Thomas
Thomas 2012 年 6 月 20 日
so u want row1 as column, then row2 into column under it and so on ?
Andrew Alkiviades
Andrew Alkiviades 2012 年 6 月 20 日
yes row 1 as column, row 2 as column and then, the new column 2 under column 1
Thomas
Thomas 2012 年 6 月 20 日
try
reshape(permute(PV_power_output,[2 1 3]),8760,1,3)
Andrew Alkiviades
Andrew Alkiviades 2012 年 6 月 20 日
can this be written in terms of K (since I will be adding more dimensions later on as:
out = reshape(permute(PV_power_output,[2 1 K]),8760,1,K);
Thomas
Thomas 2012 年 6 月 20 日
for reshape the product of dimension should be equal i.e. 365x24x3 == 8760x1xk (so k=3)
Walter Roberson
Walter Roberson 2012 年 6 月 20 日
The second argument to permute() should list all of the dimension numbers. You could use
permdims = 1 : ndims(PV_power_output);
permdims(1:2) = permdims(2:1);
out = reshape(permute(PV_power_output, permdims),8760,1,[]);
Walter Roberson
Walter Roberson 2012 年 6 月 20 日
Are you adding more dimensions, or making the third dimension larger?
Andrew Alkiviades
Andrew Alkiviades 2012 年 6 月 20 日
no I am to add more dimensions later (or at least have the flexibility to add/remove dimensions) of the same size
Andrew Alkiviades
Andrew Alkiviades 2012 年 6 月 20 日
I get an error on the perm dims(1:2) line of
"Improper assignment with rectangular empty matrix"
Cant I just transpose the matrix (for each dimension) and then vectorize it using the colon (:) somehow?
Andrew Alkiviades
Andrew Alkiviades 2012 年 6 月 20 日
What does the (1:2) represent?
Walter Roberson
Walter Roberson 2012 年 6 月 20 日
Sorry should be
permdims([1 2]) = permdims([2 1]);
These are indices into permdims, which in turn is 1 to the dimension number, so the result is to get
[2 1 3 4 5 6 .... dimension number]

サインインしてコメントする。

その他の回答 (1 件)

Andrew Alkiviades
Andrew Alkiviades 2012 年 6 月 20 日

0 投票

I have managed to get it working for K = 1
I have transposed the matrix then stacked the columns. However, I need to code for the three sets of matrices - at the moment I can only access K = 1 Does anyone have any ideas on how to modify the code for the K = 2 and 3?
PV_power_output(:,:,K) = real((Vmpp_(:,:,K).*Impp_(:,:,K))*inverter_efficiency)/1000; % Power output in kW
PV_energy(:,:,K) = sum(sum(PV_power_output(:,:,K))) ; % Energy output in kWh
PV_out(:,:,K) = [real((Vmpp_(:,:,K).*Impp_(:,:,K))*inverter_efficiency)/1000]'; % Power output in kW
out = PV_out(:);

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by