matching elements between two arrays

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 Kommentare

Azzi Abdelmalek
Azzi Abdelmalek am 28 Aug. 2013
Give a short numeric example , (10 rows for example) with the expected resullt
Walter Roberson
Walter Roberson am 28 Aug. 2013
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 am 29 Aug. 2013
You are right. It was my mistake! I corrected it and i have done successfully what i wanted! Thank you all for your contribution!

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

dpb
dpb am 28 Aug. 2013
Bearbeitet: dpb am 28 Aug. 2013

0 Stimmen

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 Kommentare

George
George am 28 Aug. 2013
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 am 28 Aug. 2013
cell2mat( arrayfun(@(i) repmat(NG(i),24,1), 1:length(NG), 'uniformoutput', false))
Yes you can use a loop.
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))

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 28 Aug. 2013

0 Stimmen

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

2 Kommentare

George
George am 28 Aug. 2013
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?
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 ?

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 28 Aug. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by