create new variable in dataset based on existing variable

Hello, My dataset has two columns : Hour (1:24) and Prices. I want to create a new variable called "peak type" based on the value in the Hour column. Specifically - if the hour is in the set (1 through 6,23,24) then the peaktype is off peak, else the peaktype is peak. For starters, I tried this just on hour 1 using the code below:
% myDset is 24x2 dataset (column 1 = Hour, Column 2 = Price) for x = 1:24; if myDset{x,1}==1; pt(x,1)='7x8'; else; pt(x,1)='5x16'; end; end
error: Assignment has more non-singleton rhs dimensions than non-singleton subscripts
I'm fairly new to Matlab and I'm not sure what's going on. I keep getting errors on other variants of the above code. Can anyone help please? Thanks Chet

 採用された回答

Mischa Kim
Mischa Kim 2014 年 3 月 20 日

1 投票

Chetan, check out:
myDset = num2cell([(1:24)', rand(24,1)]); % create some data
for x = 1:24
if any(myDset{x,1} == [1:6,23,24]) % check for peak hour
pt{x} = '7x8'; % save string in cell aray
else
pt{x} = '5x16';
end
end
disp(pt)

1 件のコメント

Chet
Chet 2014 年 3 月 20 日
Thanks Mischa! This worked perfectly!
Chet

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

その他の回答 (0 件)

カテゴリ

質問済み:

2014 年 3 月 19 日

コメント済み:

2014 年 3 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by