How can I make an array using for loop?

조회 수: 8 (최근 30일)
Ashfaq Ahmed
Ashfaq Ahmed 2023년 2월 15일
댓글: Stephen23 2023년 2월 15일
Hi all!
I am trying to prepare a big array by adding all the cells from Time (18x1 cell array) like this -
TimeSeries = [Time{1,1};Time{2,1};Time{3,1};Time{4,1};Time{5,1};...
Time{6,1};Time{7,1};Time{8,1};Time{9,1};Time{10,1};...
Time{11,1};Time{12,1};Time{13,1};Time{14,1};Time{15,1};...
Time{16,1};Time{17,1};Time{18,1}];
But how can I prepare the TimeSeries array without being too manual about it?
The solution doesn't necessarily have to be a for loop approach. Any other approach is welcome! :)

채택된 답변

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 2월 15일
It is advised to use timetable instead of timeseries(). Therefore, it is straightforward to create timetable using array2timetable(), e.g.:
A =(1:18)';
AS = seconds(rand(size(A))); % Duration
TT = array2timetable(A, 'RowTimes',AS) % TimeTable() with 18 rows and two columns
TT = 18×1 timetable
Time A ____________ __ 0.37307 sec 1 0.68903 sec 2 0.9241 sec 3 0.19051 sec 4 0.65233 sec 5 0.42433 sec 6 0.25006 sec 7 0.083402 sec 8 0.37205 sec 9 0.37763 sec 10 0.23997 sec 11 0.052084 sec 12 0.54406 sec 13 0.20393 sec 14 0.94033 sec 15 0.55957 sec 16
T = removevars(TT, 'A') % 18 by 1 empty TIMETABLE
T = 18×0 empty timetable
% Alt. way to create timeseries is:
B =(1:18)';
TS = timeseries(B)
timeseries Common Properties: Name: 'unnamed' Time: [18x1 double] TimeInfo: tsdata.timemetadata Data: [18x1 double] DataInfo: tsdata.datametadata
  댓글 수: 5
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 2월 15일
As now you question is a bit different from the initial one. if the latter one what you want, this is how one can create a cell array with some values:
M=(1:18).';
T= cell(size(M)); % EMpty cell 18-by-1
for ii = 1:length(M)
T{ii}=M(ii); % Cell with elements from M matrix array
end
T
T = 18×1 cell array
{[ 1]} {[ 2]} {[ 3]} {[ 4]} {[ 5]} {[ 6]} {[ 7]} {[ 8]} {[ 9]} {[10]} {[11]} {[12]} {[13]} {[14]} {[15]} {[16]}
T{:}
ans = 1
ans = 2
ans = 3
ans = 4
ans = 5
ans = 6
ans = 7
ans = 8
ans = 9
ans = 10
ans = 11
ans = 12
ans = 13
ans = 14
ans = 15
ans = 16
ans = 17
ans = 18
Stephen23
Stephen23 2023년 2월 15일
"I was trying to communicate with him because I thought I needed to explain my question a bit more. "
The only thing that accepting an answer communicates is that you are happy with the information given in that answer. Yet in your very first comment you wrote "Thank you for your response but unfortunately, it did not answer my question". Very confusing.

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

추가 답변 (1개)

Stephen23
Stephen23 2023년 2월 15일
  댓글 수: 1
Ashfaq Ahmed
Ashfaq Ahmed 2023년 2월 15일
Omg thank you so much Stephen, look what change you made in my code!!
Haha! That was incredible. Thanks again.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by