Matrix: Find corresponding elements in a different column

6 views (last 30 days)
I'm attempting to analyze a file with four columns of data, and each column has an equivalent number of rows. I need to split up these four large columns into smaller arrays. How could I find the elements in column 'B' which correspond to elements in column 'A'?
For instance, column 1 lists temperature and column 2 lists the pressure which corresponds to that temperature. Let's say the temperature column ranges from 0 to 100. I've split this large temperature column into smaller columns, such that column 1 lists temperatures 1-10, column 2 lists temperatures 11-20, etc. How can I create arrays which list pressure up to the point where the temperature array cuts off?
This seems trivial for small data sets, but mine have a little over 8,000 rows. I've thought of creating a large matrix with the four columns, but I don't know how to define array in which, for example, the pressure element (xxx, 2) corresponds to the temperature element (xxx, 1). Any help would be greatly appreciated. Thank you!
  3 Comments
Jack
Jack on 2 May 2013
Edited: Jack on 2 May 2013
I used a logical operator. For example, I defined
Temp_2 = Mat(Temp>10 & Temp<=20);
where Mat is the large matrix with 4 columns.
For other columns, I'm not sure how to define the array with the corresponding elements. Temperature is easy because it rises linearly, and I can use the logical operator; pressure, however, is not linear.
The original array can still be accessed.
I was thinking I could figure out how large each temperature sub-array is and then define sub-columns for the other 3 columns according to the size of each temperature sub-array. That is, if Temp_1 has elements spanning from row 0 to 13, then Pres_1 has elements spanning from row 0 to 13. If Temp_2 has elements spanning from row 14-55, Pres_2 has elements spanning from row 14-55. I just don't know how to determine which elements of the original large array compose each temperature sub-array.
Jack
Jack on 2 May 2013
Edited: Jack on 2 May 2013
I'll give an example with random data:
I can select temperature in the range from 1 to 10 using
Temp_1 = Temp(Temp>=0 & Temp<=10); This has 8 elements I can do the same for temperature greater than 10 to 20, and the array will have 5 elements
My question is: How can I define 2 arrays for pressure so that they have elements corresponding to the elements in the 2 temperature arrays? e.g. Pres_1 will be [0.33 0.32 0.53 0.26 0.12] and Pres_2 will list the remaining pressure values.

Sign in to comment.

Accepted Answer

Cedric
Cedric on 3 May 2013
Edited: Cedric on 3 May 2013
Ok, I understand; do you need columns to be separate? If not, you could go for the following solution: assume the whole dataset is stored in an array called data, whose 1st column contains temperatures, 2nd column pressures, etc.
% Build a vector of logicals flagging relevant rows.
id = data(:,1) >= 0 & data(:,1) < 10 ;
% Extract relevant block of data, taking relevant rows and all columns.
data_0_10 = data(id,:) ;
Note that if you had already split data into temperature, pressure, etc, vectors, you could work the same way:
temperature = data(:,1) ;
pressure = data(:,2) ;
id = temperature >= 0 & temperature < 10 ;
temp_0_10 = temperature(id) ;
pres_0_10 = pressure(id) ;
Finally, note that there would be ways to split your dataset in a more concise manner than iterating through ranges. They are a little more complicated, but let me know if you'd like to go for this.
EDIT: I took 2 more minutes to illustrate a more complex way to split your dataset into a cell array.
>> blockSize = accumarray(1 + floor(data(:,1)/10), ones(size(data(:,1)))) ;
>> block = mat2cell(data, blockSize, size(data,2)) ;
Here, block{1} contains an array with all rows of data that have a temperature in the range [0,10[, block{2} contains an array with all rows of data that have a temperature in the range [10,20[, etc. As you can see, in two lines (that could be compressed in a single line), we split the whole dataset. Let me know if you want more information about this solution.
  3 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Numeric Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!