I am looking for a fastest way to copy some special value of a matrix to other matrix. Assume that I have matrix A such as

```
A =[4     1     5     4     4
   -2    -1     1     2     2
    3    -1     1     7     3
    5     3    -1     1    -2
    6     4     4    -1     1]

```

My aim is that copy element that have value 1 and -1 to matrix B. The expected matrix B such as

```
B =[ 0     1     0     0     0
     0    -1     1     0     0
     0    -1     1     0     0
     0     0    -1     1     0
     0     0     0    -1     1]

```

I performed two way to create matrix B. However, I think that my way is still not fastest way if size of matrix A becomes larger. I know that the forum has many expert matlab guy. Could you suggest to me another way?
This is my code

```
%%First way:
tic;B=((A==1)|(A==-1)).*A;toc
Elapsed time is 0.000026 seconds.
%%Second way:
tic;idx1=find(A==1);idx2=find(A==-1);B=zeros(size(A));B(idx1)=1; B(idx2)=-1;toc;B
Elapsed time is 0.000034 seconds.

```