I have a matrix 2 x N (lets call it MyMatrix) containing pairs of elements (element in (1,1) corresponds to element (2,1), element in (1,2) correspords to element (2,2) and so on.) Entries in first row are sorted in ascending order. What I would like to do is split this matrix into 2 matrices 2 x K and 2 x N-K. First matrix will contain part of MyMatrix where entries in row 1 are less than some given value (in my example it will be (max-min)/2 , where max = maximum value in row 1, min = minimum walue in row 1) and second matrix will consist of the rest of MyMatrix. I'm sorry if it is confusing but I tried my best to explain to you what I would like to achieve.

Here is an example:

MyMat =

    |1   2     4    6   13   52   65   120    125|
    |4   132   53   1   64   34   5    2      66 |

min = 1 , max = 125, avg = (125-1)/2 = 62.

so result will be as follows:

a =

|1   2     4    6   13   52 |
|4   132   53   1   64   34 |

b=

|65   120   125|
|5    2     66 |

Thanks in advance for your help.

Kind regards,

Tom.

有帮助吗?

解决方案

You can simply do

a=MyMat(:,MyMat(1,:)<avg);
b=MyMat(:,MyMat(1,:)>=avg);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top