Ok stuck here. Pls gives some help.
dataTrain is a table of all my data. This is a time series dataset. It has more than 20 variables. Variable 1 is the individual no. There are 'n' = 260 individuals. Variable 2 is time. Variable No. 3 is the 'Output' while No. 4 to end are the 'Inputs'
I want to divide all of this data into cells of Xtrain(Inputs) and Ytrain(Output) according to individual no.
This is my code
Xtrain = cell(n,1);
Ytrain = cell(n,1);
i = 1;
j = 1;
k = 1;
for j = 1:n;
while dataTrain{i,1} == j;
L(:,i)= dataTrain{i,4:end};
M(:,i)= dataTrain{i,3};
i = i + 1;
end
Xtrain{j,:} = L(:,k:i-1);
Ytrain{j,:} = M(:,k:i-1);
k = i;
j = j + 1;
end
However when I do this, Xtrain and Ytrain stopped at 259, Matlab gives 'Row index exceeds table dimensions.'

6 件のコメント

Walter Roberson
Walter Roberson 2019 年 8 月 28 日
What happens if you get to i == 260 and dataTrain{260,1) == j ?
Ahmad Kamal Bin Mohd Nor
Ahmad Kamal Bin Mohd Nor 2019 年 8 月 28 日
編集済み: Ahmad Kamal Bin Mohd Nor 2019 年 8 月 28 日
J will arrive at max value 260 but the loop stop at the first end loop, making i and k will not go to max
Xtrain = cell(n,1);
Ytrain = cell(n,1);
i = 1;
j = 1;
k = 1;
for j = 1:n; <-----j = 260 max value achieve
while dataTrain{i,1} == j;
L(:,i)= dataTrain{i,4:end};
M(:,i)= dataTrain{i,3};
i = i + 1;
end <------j = 260 program end
Xtrain{j,:} = L(:,k:i-1);
Ytrain{j,:} = M(:,k:i-1);
k = i;
j = j + 1;
end
the cyclist
the cyclist 2019 年 8 月 28 日
Can you upload your data in a MAT file, so that we can actually run your code?
the cyclist
the cyclist 2019 年 8 月 28 日
編集済み: the cyclist 2019 年 8 月 28 日
Could it be that the following condition
while dataTrain{i,1} == j
is not met because the value in dataTrain is a floating-point number that is very close to, but not exactly, equal to j, so the while loop exits before you expect?
Ahmad Kamal Bin Mohd Nor
Ahmad Kamal Bin Mohd Nor 2019 年 8 月 28 日
here is the MAT file with the dataTrain table
you can use the code I had given to test
Ahmad Kamal Bin Mohd Nor
Ahmad Kamal Bin Mohd Nor 2019 年 8 月 28 日
if you run the code, it will take some time.

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

 採用された回答

Jon
Jon 2019 年 8 月 28 日
編集済み: Jon 2019 年 8 月 28 日

0 投票

Your looping logic looks quite complicated, and it is difficult to follow exactly what your intent is here. From your verbal description it sounded like you just want to make two cell arrays one with the input data and the other with the output data.
So is what you are trying to do just as simple as the following or am I not understanding what you are trying to accomplish (I'm assuming you want the individual number and time in both arrays)
Xtrain = horzcat(dataTrain(:,1:2),dataTrain(:,4:end))
Ytrain = dataTrain(:,1:3)
I would also recommend that for this type of data you are much better of either using MATLAB tables or if infact this is actually time series data use a timetable
For more info, on the command line enter
doc timetable
or enter
doc table

5 件のコメント

Ahmad Kamal Bin Mohd Nor
Ahmad Kamal Bin Mohd Nor 2019 年 8 月 28 日
perhaps with this MAT file data it will be clearer. You could run the code I had given with this MAT file.
I am not sure whether what you had written is the one Im searching for (I think I understand what you have written)
In the table, fleet means individual. So you will have 260 fleets
Ahmad Kamal Bin Mohd Nor
Ahmad Kamal Bin Mohd Nor 2019 年 8 月 28 日
when u run the code, it will takes a moment. it will not be fast.
Jon
Jon 2019 年 8 月 28 日
編集済み: Jon 2019 年 8 月 28 日
Oh, so you already have your data in a table.
I would recommend just working with your data in this existing table. It is generally not necessary or desirable to break up a large overall table into individual tables. You can use indexing and grouping variables to collect the subsets of data that you want for particular parts of your analysis.
So for example if at some point in your analysis you wanted what you call the input data and output data for Fleet number 213 as numerical arrays you could just use
xTrain = dataTrain(dataTrain.Fleet==213,[2,4:27])
yTrain = dataTrain(dataTrain.Fleet==213,2:3)
where I have included the Cycle (time) column in each, but omitted the Fleet number (since presumably you would know what that was since that was the subset you selected.
The above could of course be included in some type of loop that did the analysis for each fleet member, in which case, if your loop index was k, you would substitue dataTrain.Fleet == k for dataTrain.Fleet == 213
Does this help? If you are still stuck, please say a little more about what you want to do with the data and what subsets of the data you need to extract.
Ahmad Kamal Bin Mohd Nor
Ahmad Kamal Bin Mohd Nor 2019 年 8 月 28 日
wow... this works
thanks!!!!
Jon
Jon 2019 年 8 月 28 日
Excellent! I've found that working with MATLAB tables has hugely simplified a lot of data analysis that I do.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2019 年 8 月 28 日

コメント済み:

Jon
2019 年 8 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by