필터 지우기
필터 지우기

How to split a [30x250x1000] array into 1,000-2D matrices

조회 수: 1 (최근 30일)
Kevin Brinneman
Kevin Brinneman 2019년 2월 10일
댓글: Kevin Brinneman 2019년 2월 10일
Hello everyone,
I have a dataset of size 30x250x100. I need to feed this array into a function 1,000 times. This is the for loop that I am using for the job. Where data_class will be the 30x250x1000 input array and number_epoch 1000.
Here i am trying to create 1,000 epoch_seg variables. Each one with 30 by 250 matrices given the 1,000 but can't figure out how to update epoch_seg to contain each matrix for every time step.
function epoch = matrix_extractor(data_class,number_epochs)
data_class;
for k=1:number_epochs
epoch(:,:)=data_class(:,:,k);
epoch_seg[k]=epoch(:,:)
end;
end
Thank you!
  댓글 수: 3
Stephen23
Stephen23 2019년 2월 10일
"Here i am trying to create 1,000 epoch_seg variables."
Dynamically creating or accessing variable names is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know some of the reasons why:
Instead of forcing yourself inrto writing inefficient, complex code, you should just use indexing. Indexing Is neat, simple, and very efficient. Unlike what you are trying to do.
Kevin Brinneman
Kevin Brinneman 2019년 2월 10일
Sure, will read the document. Thanks

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

채택된 답변

Stephen23
Stephen23 2019년 2월 10일
편집: Stephen23 2019년 2월 10일
Just use a simple cell array:
function epoch = matrix_extractor(data_class,number_epochs)
data_class;
C = cell(1,number_epochs);
for k = 1:number_epochs
C{k} = data_class(:,:,k);
end
end
Note that writing this is a waste of time: just use num2cell or mat2cell.
Note that there is likely little point to this anyway: it would be simpler to just access the original data array using indexing, rather than splitting up and duplicating that data in memory. Splitting up data rarely makes processing data easier.
  댓글 수: 1
Kevin Brinneman
Kevin Brinneman 2019년 2월 10일
Yes I had the intention to just access the data but couldnt find a better way. Thanks for the help!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by