To change value in cells based on conditions

조회 수: 1 (최근 30일)
yue ishida
yue ishida 2012년 1월 5일
I have coding as below.
a=[11 11 33 33 22 44; 33 33 33 11 11 22; 33 33 11 22 22 44; 44 44 33 22 44 11]
I need to change the matrix value based on this conditions and cannot overlap each other:
1. if value 33 occured 3 times continuosly, the end of 33 need to replace with 44
33 33 33 = 33 33 44
2. If value 33 appeared 2 times continously and followed by 22, I need to replace the 22 value with 44 too.
33 33 22 = 33 33 44
2. if value 33 occured then followed by value 11, the value 11 will change into 22
33 11 = 33 22
Therefore, origin matrix will become like this:
c=
11 11 33 33 44 44
33 33 44 11 11 22
33 33 44 22 22 44
44 44 33 22 44 11
I need help to code this problem. Thank you for your help.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 1월 5일
n = size(a,1);
K = reshape([a zeros(n,1)].',[],1)';
K(strfind(K,[33 33 33])+2) = 44
K(strfind(K,[33 33 22])+2) = 44
K(strfind(K,[33 11])+1) = 22
out = reshape(K,[],n)';
out = out(:,1:end-1);
ADDED [22:16(UTC+4) 05.01.2012]
n = size(a,1);
A = reshape([a zeros(n,1)].',[],1).';
N = numel(A);
j1 = 1;
while j1 <= N
if j1+2 <= N && (all(A(j1:j1+2) == [33 33 33]) ...
|| all(A(j1:j1+2) == [33 33 22]) ...
|| all(A(j1:j1+2) == [33 33 11]))
A(j1+2) = 44;
j1 = j1+3;
elseif j1+1 <= N && all(A(j1:j1+1) == [33 11])
A(j1+1) = 22;
j1 = j1+2;
else
j1 = j1 + 1;
end
end
out = reshape(A,[],n)';
out = out(:,1:end-1);
  댓글 수: 3
Andrei Bobrov
Andrei Bobrov 2012년 1월 5일
Hi Jan! I am agree with you.
Added variant of code.
yue ishida
yue ishida 2012년 1월 6일
thank you very much.

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

추가 답변 (3개)

Walter Roberson
Walter Roberson 2012년 1월 5일
Use logical vectors for the various conditions. If you think about logical vectors a bit you will come up with a simple way to prevent overlaps between the conditions.
Question:
What should be the output for 33 33 33 33 11
  댓글 수: 2
yue ishida
yue ishida 2012년 1월 5일
the output is 33 33 44 33 22
Walter Roberson
Walter Roberson 2012년 1월 5일
Okay... it just wasn't clear whether your mention of three 33's was exactly three or was "three or more".

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


yair suari
yair suari 2012년 1월 5일
something like (not very efficient)
for i=1:size(a,1) if a(i:i+2)==[33 33 33];a(i:i+2)=[33 33 44] end
  댓글 수: 1
Jan
Jan 2012년 1월 5일
Or: "a(i+2) = 44" - there is no need to overwrite 33 by 33.

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


yue ishida
yue ishida 2012년 1월 5일
i still get error for my answer, maybe we need to change a bit of this coding? I was trying this code based on your code, but still don't get the answer...
for i=1:size(a,1)
if a(i:i+2)==[33 33 33]
a(i:i+2)=[33 33 44];
elseif a(i:i+2)==[33 33 22]
a(i:i+2)=[33 33 44];
elseif a(i:i+1)==[33 11]
a(i:i+1)=[33 22];
end
end
  댓글 수: 2
Jan
Jan 2012년 1월 5일
if all(a(i:i+2)==[33 33 33]) % better an explicite ALL
a(i+2)=44; % No need to overwrite the 33 with 33
...
yue ishida
yue ishida 2012년 1월 6일
thanks for guide...

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by