how to create a structure?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I have a data set (DATA) that contains 100 stations with daily prices and demand.
Using this code I can have 100 stations with the data that belong to each of them.
Can you help me to fix the code??
N=length(DATA(:,1))
for i=1:N
x=DATA(i,1)
station=['S', num2str(x)]
station=[station;DATA(i,:)]
end
1 件のコメント
Stephen23
2019 年 11 月 5 日
Using numbered variables is a sign that you are doing something wrong.
Indexing is simple and efficient (unlike what you are trying to do).
採用された回答
Walter Roberson
2019 年 11 月 5 日
You can use num2cell to split the data into cell array by column.
You can construct an array of field names in a couple of ways, including using sprintfc or string objects with the + (concatenation) operation.
Once you have both of the above you can use cell2struct to create your structure.
However we wonder why you are not just leaving the data in the numeric matrix, as that is the fastest and smallest storage?
12 件のコメント
Thanks Walter Roberson!Yeah, I can leave the data as in the matrix that I uploaded below but it takes time to write a code for each station. Hence, I want to split the stations and then calculate the mean price and demand for each month in each station.
I uploaded the data if you can help me with a simple code.
Walter Roberson
2019 年 11 月 5 日
Why write code for each station when you could just loop?
Calculating using a struct array is not going to be faster than using a numeric array.
gjashta
2019 年 11 月 5 日
In the code above I create a for loop but is not working for this data set.
Walter Roberson
2019 年 11 月 5 日
In your loop replace
station=[station;DATA(i,:)]
With
MyStructureThatIsJustGoingToMakeEverythingHarder.(station) =DATA(i,:);
"How can I create a structure like in this link:"
What you are creating is nothing like the structure in that link. Instead of creating a non-scalar structure you are using slow, complex, inefficient code to generate multiple structures.
It is very unlikely that creating variable names dynamically is a good solution to whatever you are trying to achieve. Much better would be if you explained what your actual goal is.
Walter Roberson
2019 年 11 月 5 日
N=length(DATA(:,1))
for i=1:N
x=DATA(i,1)
stations(i).station_number = x(1);
stations(i).month = x(2);
stations(i).day = x(3);
stations(i).price = x(4);
stations(i).quantity = x(5);
end
But possibly you are trying to aggregate by station number? If so then what information do you want to group on? Your earlier discussion implies you want to group by month.
Have you ever considered just using accumarray() or grpstats() ?
[unique_stations, ~, station_idx] = unique(DATA(:,1));
array_idx = [station_idx, DATA(:,2)];
mean_price_by_month = accumarray(array_idx, DATA(:,4), [], @mean);
total_demand_by_month = accumarray(array_idx, DATA(:,5));
total_sales_by_month = accumarray(array_idx, DATA(:,4).*DATA(:,5));
No structure needed. The output in each case would be a 2D array, one row for each unique station ID, and one column for each month. The contents would be the mean price, the total units, and the total dollar sales, depending on the variable.
Walter Roberson
2019 年 11 月 5 日
[~, ~, id_idx] = unique(DATA(:,2), 'row');
num_id = length(id_idx);
stations(id_idx) = struct('station_number', {[]}, 'month', {[]}, price', {[]}, 'quantity', {[]});
for i = 1 : num_id
x = DATA(i,:);
stations(i).station_number = x(1);
stations(i).month = x(2);
stations(i).price(end+1) = x(4);
stations(i).quantity(end+1) = x(5);
end
There, a struct all aggregated by month and station. And now... ?
Walter Roberson
2019 年 11 月 5 日
編集済み: Walter Roberson
2019 年 11 月 5 日
For the structure version (not recommended):
mask = [stations.station_number] == specific_station & [stations.month] == specific_month;
length(stations(mask).price)
For the accumarray version:
day_counts = accumarray(array_idx, 1);
mask = ismember(unique_stations, specific_stations);
day_counts(mask,specific_month)
It’s a bit hard for me to understand your code but thank you very much for the time you spent Walter and your help.
Walter Roberson
2019 年 11 月 5 日
I would suggest that you spend some time studying the documentation for accumarray(). It can take a bit of getting accustomed to, but it can be very useful.
Alternately you might want to read about findgroups(), and grpstats(), and splitapply(), which can do similar tasks in ways that are sometimes less cryptic.
(If you do use splitapply() then be sure to use @(x) mean(x,1) instead of just @mean, to account for the possibility that there might only be a single row of data for a combination.)
Walter Roberson
2019 年 11 月 6 日
I have not done much work with linear regression.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Time Series Events についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
