Matrix: Find corresponding elements in a different column

6 次查看(过去 30 天)
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 个评论
Jack
Jack 2013-5-2
编辑:Jack 2013-5-2
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 2013-5-2
编辑:Jack 2013-5-2
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.

请先登录,再进行评论。

采纳的回答

Cedric
Cedric 2013-5-3
编辑:Cedric 2013-5-3
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 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by