Philip Massey
Philip Massey

Reputation: 1429

Octave Mapping over Rows

I have a matrix that's m x 2:

.2 .8
.3 .7
.7 .3

...

and I would like to map the '>' operation to each row, resulting in the column like so:

0
0
1

...

However, I can't seem to find a good way to do this without removing columns and applying a custom function to the matrix in something like

res = arrayfun(@mapfunction, matrix(:,1))

function cl = mapfunction(v)
    cl = v > .5

Is there a simpler way?

Upvotes: 1

Views: 433

Answers (1)

rayryeng
rayryeng

Reputation: 104545

You can simply calculate a Boolean / logical column that checks to see whether the first column is greater than the second column for every row in your matrix:

res = matrix(:,1) > matrix(:,2);

Given your example matrix:

matrix = [.2 .8; .3 .7; .7 .3];

This is what I get:

>> res = matrix(:,1) > matrix(:,2)

res =

     0
     0
     1

Upvotes: 3

Related Questions