create multiple submatrices from larger matrix

4 vues (au cours des 30 derniers jours)
Ryan Murphy
Ryan Murphy le 30 Nov 2018
Modifié(e) : Stephen23 le 1 Déc 2018
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

Réponses (3)

Mark Sherstan
Mark Sherstan le 30 Nov 2018
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 commentaires
Ryan Murphy
Ryan Murphy le 30 Nov 2018
Actually this worked! thanks a bunch! apologies for my confusion
Image Analyst
Image Analyst le 30 Nov 2018
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?

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 30 Nov 2018
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 le 1 Déc 2018
Modifié(e) : Stephen23 le 1 Déc 2018
M = rand(5000,3); % fake data
C = mat2cell(M,50*ones(100,1),3);
for k = 1:50
C{k}
end

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by