how to create a structure?

조회 수: 1 (최근 30일)
gjashta
gjashta 2019년 11월 5일
댓글: Walter Roberson 2019년 11월 6일
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
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
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
Walter Roberson
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
Walter Roberson 2019년 11월 6일
I have not done much work with linear regression.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Time Series Events에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by