COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Changing value in a Matrix

• Follow

```Why isn't this working?

B=[1 0 1 1 0 1 1 1 0 1 1];

B(B==0)=1;
B(B==1)=-1;

X=(1:11);
subplot(3,1,1);
plot(X,B);

I want to change each of the value in the matrix B from 0 to 1 and 1 to -1 and then plot them. I have also tried using for loop as well:

B=[1 0 1 1 0 1 1 1 0 1 1];
[row,column]=size(B);

for c=1:column;
if B(c)==0;
B(c)=1;
end
if B(c)==1;
B(c)=-1;
end
end

X=(1:11);
subplot(3,1,1);
plot(X,B);
```
 0

```Dave Smith
A little work with the debugger would have easily pointed it out.
After you did
B(B==0)=1;
your whole B array was now equal to 1
then the line
B(B==1)=-1;
set the whole array (because every element was now 1) to -1.
Try reversing the lines:
B(B==1)=-1;
B(B==0)=1;

```
 0

```ImageAnalyst <imageanalyst@mailinator.com> wrote in message <e13e9f72-dda5-4a39-9489-44bd7c00d415@z4g2000yqa.googlegroups.com>...
> Dave Smith
> A little work with the debugger would have easily pointed it out.
> After you did
> B(B==0)=1;
> your whole B array was now equal to 1
> then the line
> B(B==1)=-1;
> set the whole array (because every element was now 1) to -1.
> Try reversing the lines:
> B(B==1)=-1;
> B(B==0)=1;

Thank so much. So simple yet I been struggling with it for hours.
```
 0

2 Replies
212 Views

Similiar Articles:

7/17/2012 4:11:53 AM