3
votes

Assume we have the following data:

H_T = [36 66 21 65 52 67 73; 31 23 19 33 36 39 42]
P   = [40 38 39 40 35 32 37]

Using MATLAB 7.0, I want to create three new matrices that have the following properties:

The matrix H (the first part in matrix H_T) will be divided to 3 intervals:

  • Matrix 1: the 1st interval contains the H values between 20 to 40
  • Matrix 2: the 2nd interval contains the H values between 40 to 60
  • Matrix 3: the 3rd interval contains the H values between 60 to 80

The important thing is that the corresponding T and P will also be included in their new matrices meaning that H will control the new matrices depending on the specifications defined above.

So, the resultant matrices will be:

H_T_1 = [36 21; 31 19]
P_1   = [40 39]

H_T_2 = [52; 36]
P_2   = [35]

H_T_3 = [66 65 67 73; 23 33 39 42]
P_3   = [38 40 32 37] 

Actually, this is a simple example and it is easy by looking to create the new matrices depending on the specifications, BUT in my values I have thousands of numbers which makes it very difficult to do that.

1

1 Answers

2
votes

Here's a quick solution

[~,bins] = histc(H_T(1,:), [20 40 60 80]);

outHT = cell(3,1);
outP = cell(3,1);

for i=1:3
    idx = (bins == i);
    outHT{i} = H_T(:,idx);
    outP{i} = P(idx);
end

then you access the matrices as:

>> outHT{3}
ans =
    66    65    67    73
    23    33    39    42
>> outP{3}
ans =
    38    40    32    37