user2171003
user2171003

Reputation: 41

Matlab - replacing all negative and positive values

I have a dataset with many channels and many trials (each corresponding to a separate .mat file or data array).

I want to replace all negative values with -1 and all positive values with + 1 for a single row (for i = 1:n (# of trials) replace all values positive values in (22,:) with +1 and all negative values with -1).

Hope that's clear.

Thanks a lot!

Upvotes: 1

Views: 5566

Answers (2)

Vuwox
Vuwox

Reputation: 2359

If A is your matrix NxN.

Do

A(X,:) = sign(A(X,:));

Where X is the row you want to change.

Upvotes: 6

Roney Michael
Roney Michael

Reputation: 3994

If I understood you correctly, all you need to do is this, assuming your input matrix is A and that you want to change the values in it's 22nd row:

A(22,(A(22,:)<0)) = -1;
A(22,(A(22,:)>0)) = 1;

For example:

>> A = randint(25,5,[-10,10]);
>> A(22,:)

ans =

   -10    -1    -5     1    10

>> A(22,(A(22,:)<0)) = -1;
>> A(22,(A(22,:)>0)) = 1;
>> A(22,:)

ans =

   -1    -1    -1     1    1

Upvotes: 0

Related Questions