Extract and rearrange data in Matlab

  • Context: MATLAB 
  • Thread starter Thread starter geoffrey g
  • Start date Start date
  • Tags Tags
    Data Matlab Rearrange
Click For Summary

Discussion Overview

The discussion revolves around rearranging data in MATLAB, specifically transforming a dataset from an Excel file into a desired output format. Participants explore methods for summing and averaging values based on specific conditions across multiple columns of data.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant describes the need to sum values from column 3 based on conditions from columns 1 and 2, and to calculate the mean of values from column 4 for each unique value in column 1.
  • Another participant requests clarification on the example provided, noting inconsistencies in the number of values in the output rows.
  • A participant explains the real-life context of the data, detailing what each column represents in terms of transect numbers, species codes, animal counts, and ice cover percentages.
  • A participant offers to create a MATLAB script (m-file) to process the data, suggesting that it may need adjustments for the user's specific dataset.
  • One participant provides a sample MATLAB code that processes the data as requested, including handling NaN values and preallocating output matrices.
  • Another participant expresses gratitude for the assistance and indicates they will test the provided code.

Areas of Agreement / Disagreement

Participants generally agree on the approach to rearranging the data, but there are some clarifications needed regarding the example format and the specifics of the dataset. No consensus is reached on the final implementation details as participants are still discussing and refining the code.

Contextual Notes

The discussion includes assumptions about the structure of the data and the need for potential adjustments to the provided MATLAB code to fit the user's actual dataset, which may have more columns and different characteristics.

geoffrey g
Messages
6
Reaction score
0
Hi all,

I need to rearrange my data. I have an excel file with 12 columns and 2045 rows, which I transformed that into a .mat.

I need to query my data rearranged as follows (cf. example matrix below):

1-sum all values of Col 3 if:
a-they belong to a given value of Col 1 and to a given value in Col 2.
2-mean of values of Col 4 for each given value of Col 1

3-NaN's where conditions above are not met
4-all saved in a new matrix

The difficulty is to make this work for the 2045 rows of my database. I guess it needs a for-loop that reiterates the calculations and at each step saves the values in a matrix.

Here is an example:

input matrix A:

1 NaN NaN 20
1 2 4 10
1 2 3 15
1 NaN NaN 10
1 5 2 25
1 6 1 10
1 2 5 5
2 NaN NaN 5
2 1 3 10
2 1 2 25
3 6 5 15
3 4 3 10
3 3 1 30

This is what I'd like to get:

1 2 3 4 5 6 Mean
1 NaN 12 NaN NaN 2 1 13.57
2 5 NaN NaN NaN NaN NaN 13.33
3 NaN NaN 1 3 NaN 5 18.33

I'd really appreciate your help and will include you in the acknowledgments of the 2 papers I will get out of this.

Thanks a lot!

g
 
Physics news on Phys.org
(hope) these are nicer matrices:

Input:

1 NaN NaN 20
1 2 4 10
1 2 3 15
1 NaN NaN 10
1 5 2 25
1 6 1 10
1 2 5 5
2 NaN NaN 5
2 1 3 10
2 1 2 25
3 6 5 15
3 4 3 10
3 3 1 30

desired output:

1 2 3 4 5 6 mean
1 NaN 12 NaN NaN 2 1 13.57
2 5 NaN NaN NaN NaN NaN 13.33
3 NaN NaN 1 3 NaN 5 18.33
 
Can you be more specific? The example you gave wasn't very clear, but this should be a simple problem to overcome. For ex, the first row in your "desired output" has 6 values, and the rest have 8..
 
Hi Kreil,

Sorry for the bad format...
Let me know if the attachments work...

Here is what my data means in real life:

Input matrix
Rows are individual observations
Col 1 = transect number
Col 2= species code
Col 3= count of animals of corresponding species
Col 4= % ice cover for that specific observation

Ouput:

Col 1= transect number
Col 2 to Col 7 = sum of animal counts of species 1 to species 6
Col 8= mean ice cover for corresponding transect

N=NaN

Thanks!

g
 

Attachments

  • Input matrix.png
    Input matrix.png
    3.6 KB · Views: 686
  • Desired output matrix.png
    Desired output matrix.png
    5.2 KB · Views: 758
Last edited:
I'll write up an m-file that works on that data, but you'll probably need to edit it to make it work with your actual data (since it has more columns and possibly more than 3 transects)
 
thanks so much Kreil!
:-)
g
 
I wrote this up quickly so there are likely ways you can improve it. For the time being, it returns your desired output.
Code:
%sample data matrix, A
A = [1 NaN NaN 20;1 2 4 10;1 2 3 15;1 NaN NaN 10;1 5 2 25;1 6 1 10;1 2 5 5;2 NaN NaN 5;2 1 3 10;2 1 2 25;3 6 5 15;3 4 3 10;3 3 1 30];

%Change NaN's in A to zeros for summing purposes
A(isnan(A))=0;

%Change these vars to match real counts
Transects = 3;
Species = 6;

%Preallocate output
Out = zeros(Transects,8);

for i = 1:Transects
    %First column is just transect number
    Out(i,1) = i;
    %B is the subset of A for current transect
    B = A(A(:,1)==i,:);
    %C is the subset of B for current species
    for k = 1:Species
        C = B(B(:,2)==k,:);
        Out(i,k+1) = sum(C(:,3));
    end
    %Ice cover means
    Out(i,end) = mean(B(:,4));
end

%Convert the zeros back to NaN's
Out(Out==0)=NaN;

Since a numeric matrix in MATLAB cannot have an empty entry or a char like 'mean', this output is identical to your desired output without the first row. You'll just have to remember that column 2 is species 1, column 3 is species 2, etc...
 
fantastic, Kreil! I'll try it out on monday and will let you know!
g
 
Kreil, please drop me a line:
cortoeldemalta at yahoo dot com dot ar
geoffrey
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K