I have two arrays, just one column each. In the first array i have hourly electricity prices and in the other one natural gas prices. I want to match electricity prices with NG. For example:
Electricity an array with 22848 rows, containing elec prices in the form of 24h blocks.
NG an array with 904 rows.
I want next to electricity (another column, column 2 in elec array) prices to have the NG price given that in the same 24h block we have one daily NG price.

3 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 28 日
Give a short numeric example , (10 rows for example) with the expected resullt
Walter Roberson
Walter Roberson 2013 年 8 月 28 日
If NG is 904 daily rows, then how do those rows correspond to the 22848 hourly electrical figures, considering that 22848 hourly would be 952 days ?
George
George 2013 年 8 月 29 日
You are right. It was my mistake! I corrected it and i have done successfully what i wanted! Thank you all for your contribution!

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

 採用された回答

dpb
dpb 2013 年 8 月 28 日
編集済み: dpb 2013 年 8 月 28 日

0 投票

Ignoring the problem that you don't have commensurate numbers of members of the NG vector for the length of the E vector, presuming you can deal with that to find the beginning of the 24-hr day periods (and also presuming the desired result is the same NG price of the day repeated for each day in the E hourly data vector,
cell2mat(arrayfun(@(i) repmat(NG(i),24,1),NG,'uniformoutput',false))
ERRATUM: What comes from typing instead of cut 'n paste -- used NG as the independent variable instead of the indices thereof as needs must be--watch line wrap; used continuation and still can't really prettify it.
cell2mat(arrayfun(@(i) repmat(NG(i),24,1), ... [1:length(NG)]','uniformoutput',false))
ADDENDUM:
Again as Walter points out you have a mismatch between the NG and E vectors' respective lengths as 22848/904 = 25.27... rather than an integer number. The above will leave you with 904*24 rows for the 904 days you do have a number for. Where these are to line up within the E is your problem as there's no information on that available here.

3 件のコメント

George
George 2013 年 8 月 28 日
I have just tried your solution but i receive the following error:
Subscript indices must either be real positive integers or logicals.
Error in @(i)repmat(gas(i),24,1)
Could i do this routine by writing a script with loops? for example:
elec(j,2) = gas(i,1), with step 24 for j and one for i. Is it possible?
Walter Roberson
Walter Roberson 2013 年 8 月 28 日
cell2mat( arrayfun(@(i) repmat(NG(i),24,1), 1:length(NG), 'uniformoutput', false))
Yes you can use a loop.
dpb
dpb 2013 年 8 月 28 日
Ooops, typo, sorry. The independent variable needs to be the indices of NG from 1:length(NG), not just NG itself...
cell2mat(arrayfun(@(i) repmat(NG(i),24,1), ... [1:length(NG)]','uniformoutput',false))

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 8 月 28 日

0 投票

[Electricity, reshape( NG(:,ones(1,24)), [], 1)]

2 件のコメント

George
George 2013 年 8 月 28 日
Error using horzcat Dimensions of matrices being concatenated are not consistent.
Maybe, i didn't explain it correctly. I have two separate matrixes:
Electricity 22848 x 1 NG 904 x 1
Should i add an array hours = [1;2;3;4 ...24] which will be 22848 x 1?
Walter Roberson
Walter Roberson 2013 年 8 月 28 日
As I asked above,
If NG is 904 daily rows, then how do those rows correspond to the 22848 hourly electrical figures, considering that 22848 hourly would be 952 days ?

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

カテゴリ

質問済み:

2013 年 8 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by