Andrea
Andrea

Reputation: 2764

Apply a function to each cell in Octave

I have a matrix:

>> A = rand(5,2)
ans =

   0.843985   0.911387
   0.330442   0.589956
   0.828405   0.220234
   0.049927   0.632131
   0.975574   0.254384

I want to output a new matrix of the same size (or replace the items in the matrix above) where each item is computed as follows:

So in the example above,

the output should be

   0.843985   0.911387
   0          0.589956
   0.828405   0
   0          0.632131
   0.975574   0

How to apply this function without explicitly looping through each column?

Upvotes: 0

Views: 249

Answers (1)

Phil Goddard
Phil Goddard

Reputation: 10772

>> A = rand(5,2)
A =
    0.8147    0.0975
    0.9058    0.2785
    0.1270    0.5469
    0.9134    0.9575
    0.6324    0.9649
>> B = A.*(A>0.5*max(A))
B =
    0.8147         0
    0.9058         0
         0    0.5469
    0.9134    0.9575
    0.6324    0.9649

Upvotes: 1

Related Questions