how i can put the (-1) in binary matrix with condition ?
Show older comments
how i can put (-1) after and before any group of ones in matrix like this
before code A = [ 1 1 0 0 0 1 0 1 1
0 1 1 0 0 0 0 1 1
0 0 1 1 0 0 1 0 0 ]
after code A* = [ 1 1 -1 0 -1 1 -1 1 1
-1 1 1 -1 0 0 -1 1 1
0 -1 1 1 -1 -1 1 -1 -1 ]
1 Comment
Azzi Abdelmalek
on 9 Apr 2016
Edited: Azzi Abdelmalek
on 9 Apr 2016
The last value of A* is not correct
Accepted Answer
More Answers (1)
Kuifeng
on 9 Apr 2016
% is the last number in your A* equal to 0 instead?
%the following code may work
[rows cols] = size(A);
A_diff = A(:,1:end-1)-A(:, 2:end);
A(find(A_diff == -1)) = -1;
A(find(A_diff == 1)+rows) = -1;
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!