how i can put the (-1) in binary matrix with condition ?

조회 수: 1 (최근 30일)
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016년 4월 9일
답변: Kuifeng 2016년 4월 9일
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
Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 9일
편집: Azzi Abdelmalek 2016년 4월 9일
The last value of A* is not correct

댓글을 달려면 로그인하십시오.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 9일
편집: Azzi Abdelmalek 2016년 4월 9일
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 ]
for k=1:size(A,1);
id_before=strfind(A(k,:),[0 1])
id_after=strfind(A(k,:),[1 0])+1
A(k,[id_before id_after])=-1
end

추가 답변 (1개)

Kuifeng
Kuifeng 2016년 4월 9일
% 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;

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by