create multiple submatrices from larger matrix

I have a 5000 x 3 matrix. The first column denotes temperature, and 100 data points were taken at each temperature. I want to pull out a separate 100 x 3 matrix at each temperature point. The temperature at each point is in a slightly variable range (for instance, for the first matrix at 9K, the temperature varies between 8.99 and 9.01 K). I think I need to use arrayfun but I'm not sure how to go about it. Below is what I think is a related question.
https://www.mathworks.com/matlabcentral/answers/359557-how-can-i-make-smaller-matrices-size-unknown-from-a-large-matrix

답변 (3개)

Mark Sherstan
Mark Sherstan 2018년 11월 30일
Does this work for you?
x = rand(5000,3);
count = 1;
for ii = 1:100:length(x)-100
y{count} = x(ii:ii+100,:);
count = count+1;
end
You can then extract the matrices as so:
y{1}
Which would give you 1:101 or your 100 temperature points and their corresponding values.
You will be missing some data on your last one as MATLAB starts its index at 1 and not 0.

댓글 수: 4

Thanks for the answer! This doesn't seem to be what I'm looking for however. The matrices extracted are only 2 columns and the values in the matrices aren't the same as the ones in the original matrix.
Where I'm at right now: if I put the following in, I get a 100 x 3 submatrix which contains only points taken at approx 9 K. I would like to generalize this so that it will loop and pull out 100 x 3 submatrices at the 50 different temperatures.
result=arrayfun(@(x)AAA(AAA(:,1)<9.1,:),1:1,'uni',false);
Huh? No data should be missing.
Actually this worked! thanks a bunch! apologies for my confusion
Not sure why you say that works at giving you 50 arrays of 100 rows each. What we see is:
>> y
y =
1×49 cell array
Columns 1 through 7
{101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double}
Columns 8 through 14
{101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double}
Columns 15 through 21
{101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double}
Columns 22 through 28
{101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double}
Columns 29 through 35
{101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double}
Columns 36 through 42
{101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double}
Columns 43 through 49
{101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double} {101×3 double}
You can see that you are getting 49 cells of 101 elements each. Did you see my answer where you get 50 arrays of 100 each - exactly what you requested?

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

Image Analyst
Image Analyst 2018년 11월 30일
Try this:
data = rand(5000,3);
for row = 1 : 100 : size(data, 1)-99
% Pull out a 100 row block of data.
fprintf('Extracting rows %d to %d.\n', row, row+99);
y100 = data(row:row+99, :);
% Now do something with y100.
end
You'll see
Extracting rows 1 to 100.
Extracting rows 101 to 200.
Extracting rows 201 to 300.
Extracting rows 301 to 400.
etc.
Extracting rows 4701 to 4800.
Extracting rows 4801 to 4900.
Extracting rows 4901 to 5000.
Stephen23
Stephen23 2018년 12월 1일
편집: Stephen23 2018년 12월 1일
M = rand(5000,3); % fake data
C = mat2cell(M,50*ones(100,1),3);
for k = 1:50
C{k}
end

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2018년 11월 30일

편집:

2018년 12월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by