user1403881
user1403881

Reputation: 11

set the values to binary

        for s=1:length(C_tem)
        for w=1:length(C_tem{s})
            if (abs(C_tem{s}{w}) >= 0)
               C_tem{s}{w} = 1;
            else
               C_tem{s}{w} = 0;
            end
          end
        end

I am trying to set the values larger than 0 to 1, and if less or equal to 0, but for some reason this doesn't work. I'm new in matlab, and I really need the help if possible. Thank you in advance..

Upvotes: 0

Views: 74

Answers (2)

Peter
Peter

Reputation: 14947

abs(x)>=0 is true for all values of x. The simple answer is to remove the abs. The more complete answer follows up on Dan's comment. The cell array is unnecessary at the inner level. If you instead had a cell array of regular arrays, then you could do this for the entire block of code.

for s=1:length(C_tem)
    C_tem{s} = (C_tem{s} >= 0);
end

Two things to notice: comparison operators are vectorized, meaning they return a matrix of the same size as the input, thus comparing all values at once. And, the output of the operator is 1 where true and 0 where false.

Also look at the builtin function sign to see if that's closer to what you want to do.

Upvotes: 0

Kshitij
Kshitij

Reputation: 8634

i havn't worked on matlab much but this part of code feels suspicious -

if (abs(C_tem{s}{w}) >= 0)
               C_tem{s}{w} = 1;
            else
               C_tem{s}{w} = 0;
            end

Why are you doing abs here? I think it will remove sign from number. Code should be something like this-

if (C_tem{s}{w} > 0) //I have removed abs and >= is replaced with >
               C_tem{s}{w} = 1;
            else
               C_tem{s}{w} = 0;
            end

Upvotes: 3

Related Questions