Matrix: Find corresponding elements in a different column
조회 수: 7 (최근 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
채택된 답변
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 Center 및 File Exchange에서 Numeric Types에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!