user2562952
user2562952

Reputation: 27

Matlab: How to create a CDF array

I am fairly new to Matlab and am trying to learn for school. I have created a vector of values with fixed differences between consecutive values. E.g. A = [1 2.5 4 5.5 7 8.5 10 ...].

I also have another vector of random values, e.g. B = [3 7 1 2 3 4 8 0 ...].

I want to create a new vector of the same size of A, which has numbers indicating the number of values in B which are less than or equal to each value in A.

In this example, C = [2 3 6 6 7 8 ...]

Thanks in advance!

Context: I am working on a CDF function

Upvotes: 0

Views: 140

Answers (1)

khonegger
khonegger

Reputation: 198

You can use bsxfun() to implement element-wise comparisons between arrays:

C = sum( bsxfun(@le, B', A) )

Here we're passing bsxfun() the "less than or equal to" function handle, @le . This produces a length(B) by length(A) logical array. We simply sum down the rows to get the total number of TRUE's.

Upvotes: 1

Related Questions