tags:

views:

58

answers:

1

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 new three 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.

Any help is highly appreciated.

+2  A: 

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
Amro
note that you didn't specify in your question how to deal with values outside those ranges, and whether the ranges boundaries are inclusive/exclusive??
Amro
Thanks for your replyUnfortunately, it gave me this error:??? [~,bins] = histc(H_T(1,:), [20 40 60 80]); |Error: Incomplete or misformed expression or statement.and when I wrote: help binsit gave me:bins.m not found.I am using MATLAB 7.0 and I think the function bins is not definedin this version.Actually, the values are not bounded, but I want to know howto deal with this problem and I have in my real data, the maximumvalue of H is 100, and the minimum is 1thanksregards
eng_sub
Oh I forget, just replace the tilde `~` with a dummy variable. On the other point, it depends what you want to do with values outside the ranges; the solution above just ignores those, but you can easily recover them by finding the elements with `bins==0`
Amro
thanks alot for this help.My problem is solved..thanks.best regards
eng_sub

related questions