MATLAB: create a new matrix from an existing matrix according to specifications

Suppose 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 the matrix H_T) will be divided into 3 intervals:

  • Matrix 1: the first interval contains values Hfrom 20 to 40
  • Matrix 2: the second interval contains values Hfrom 40 to 60
  • Matrix 3: the third interval contains values Hfrom 60 to 80

It is important that appropriate Tand Pwill be included in their new matrix, which means that Hwill manage the new matrices according to the specifications mentioned above.

So, the resulting 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] 

, , , , , - .

+3
1

[~,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

:

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

Source: https://habr.com/ru/post/1751671/


All Articles