MATLAB: Matching elements between two arrays

match elements between 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.

Best Answer

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.
Related Question