BillyJean
BillyJean

Reputation: 1577

changing elements in matrix

I have the following example

    a=[1 2 3; 4 5 6; 7 8 9];
    b=2*a;
    for i=1:3
        for j=1:3
            if(a(i,j)*b(i,j)<3)
                b(i,j)=0;
            end
        end
    end

I was trying to rewrite it in the following way, but it didn't work out:

    a=[1 2 3; 4 5 6; 7 8 9];
    b=2*a;
    if(a.*b < 3)
        b=0;
    end

In the last example, b is unchanged. However, I prefer that way of writing it so I can loop over matrices: Is there a way a achieve this?

Upvotes: 0

Views: 35

Answers (1)

Dan
Dan

Reputation: 45741

 a=[1 2 3; 4 5 6; 7 8 9];
 b=2*a;

 b(a.*b < 3) = 0;

Upvotes: 3

Related Questions