Matrix: Find corresponding elements in a different column

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 comentarios

Cedric
Cedric el 2 de Mayo de 2013
Editada: Cedric el 2 de Mayo de 2013
How did you order temperatures and how did you cut the large vector into smaller ones? Did you operate in a similar fashion on other columns of your large array? Finally, can you still access the original, large array, or was it lost?
I don't fully understand what you did at this point, but I can foresee a solution where you use temperatures for sorting all rows first, and then you segment all data in the same fashion (according to boundaries set on temperatures) ..
Jack
Jack el 2 de Mayo de 2013
Editada: Jack el 2 de Mayo de 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 el 2 de Mayo de 2013
Editada: Jack el 2 de Mayo de 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.

Iniciar sesión para comentar.

 Respuesta aceptada

Cedric
Cedric el 3 de Mayo de 2013
Editada: Cedric el 3 de Mayo de 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 comentarios

Jack
Jack el 3 de Mayo de 2013
Great! I've done a bit more work with this and everything works as I need. Thank you very much for your help and for your time, it's much appreciated.
Cedric
Cedric el 3 de Mayo de 2013
You're welcome!
Thank you so much Dear Wannaz,
This works great.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 2 de Mayo de 2013

Comentada:

el 17 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by