2

I have two set of numbers and want to compare and rank them relative to each other in MATLAB.

The data is:

x = [3 7 8 25 33 52 64 65 78 79 91 93];
y = [7 10 12 27 30 33 57 62 80 83 85 90];

I started with the for/if/else commands and got stuck in the middle. In other words, I want to get the answer through MATLAB how many times the numbers in the first group (x) are bigger than the ones in the second group (y).

I got started with sorting etc.

n1 = length(data1);
n2 = length(data2);

xs = sort(x);
ys = sort(y);

r1 = zeros(1,n1);
r2 = zeros(1,n2);

I am open to use other commands than this kind of sorting and for/if/else, it doesn't really matter, just need some help in the right direction.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196

2 Answers2

0

I am not entirely sure I understand what you're trying to do there. Is it safe to assume that the two vectors will be of the same size?

You could simply do an element wise division of the 2 sorted vectors and get the statistics from there. For example: div = xs./ys; max_div = max(div) mean_div = mean(div)

This is equivalent to running a for loop and dividing each element in the xs array by each element in the ys array for that corresponding index. The 'max' and 'mean' are obviously the largest quotient and the mean quotient.

Ammar Husain
  • 1,789
  • 2
  • 12
  • 26
0

In MATLAB, to find how many times each of the numbers in vector x are bigger than numbers in vector y:

sum(x > y.')

This uses the transpose of y to create a size(x) by size(y) matrix with a 1 when a number in x is greater than a number in y, then sums each column.

For your data, the result is the following vector, with an item for each number in x:

[0 0 1 3 5 6 8 8 8 8 12 12]

The vectors x and y don't have to be sorted. If you need the total number of times, just apply sum again to the result.

Terje Norderhaug
  • 3,649
  • 22
  • 25