Need Help Urgent!!!!!!!!!!!!

i have this data in matrix X=[22,33,11,33,33,33,22]
i want to change the data with this form of matrix X=[22,33,22,33,33,33,44]
i have to follow this rules: 1)when find(X==33) next data must change into 22 2)when there have data "33" three times in row,the next data must change to 44
this is my example code for the looping: [a b]=find(X==33)
if X(a,b+1)-X(a,b)==1 if X(a,b+2)-X(a,b+1)==1 X(a,b+3)=44 end end
if X(a,b+1)-X(a,b)==2 X(a,b+2)=22 end
i know im doing this all wrong,can someone give a correct algorithm to get the answer that satisfied the rules.
Amir my email: noksworld@yahoo.com

4 件のコメント

Matt Fig
Matt Fig 2011 年 3 月 28 日
Your rules conflict and don't cover all possible cases (at least for a random X). What if there are two 33's in a row? Do we change the second one to a 22? What if there are four 33's in a row? Do we change the last one to a 44, or the element after the four 33's? Please clarify.
Matt Fig
Matt Fig 2011 年 3 月 28 日
And what if the last element, or the last three elements are 33? Do we add an element?
Amir Hamzah UTeM
Amir Hamzah UTeM 2011 年 3 月 28 日
Thank Matt for replying. this is my rules
1.if only have one 33 in row,
if X=[22,33,11,33,11,11,22] change -> X=[22,33,22,33,22,11,22]
2. if there have two 33's in a row,only change the next data to 11.
if X=[22,33,22,33,33,22,22] only change next data to 11 -> X=[22,33,11,33,33,11,22]
3. if have three 33's in a row, the next data must be change to 44
if X=[22,33,22,33,33,33,22] must change into X=[22,33,22,33,33,33,44] -> X=[22,33,11,33,33,33,44]
Amir Hamzah UTeM
Amir Hamzah UTeM 2011 年 3 月 28 日
4. if there have four 33's element in row,the 4th element change to 44
5. if the last three elements are 33 in row,we must add an element to 44

サインインしてコメントする。

 採用された回答

Matt Fig
Matt Fig 2011 年 3 月 28 日

1 投票

You did not completely clarify, but here is a non-vectorized code that will do part of the job. Note that for this problem, it might be quicker to use a loop anyway.
cnt = 1;
Y = X==33;
while cnt <= length(X)-2
if Y(cnt)
if Y(cnt+1:cnt+2) % Three in a row.
X(cnt+3) = 44;
cnt = cnt + 4;
elseif Y(cnt+1) % Two in a row.
X(cnt+2) = 11;
cnt = cnt + 3;
else % Just one 33.
X(cnt+1) = 22;
cnt = cnt + 2;
end
else
cnt = cnt + 1;
end
end

5 件のコメント

Amir Hamzah UTeM
Amir Hamzah UTeM 2011 年 3 月 28 日
thanks Matt.. i think it can works with my project and i need to proceed to the next stage. catch you later,thanks a lot.. :D
Amir Hamzah UTeM
Amir Hamzah UTeM 2011 年 3 月 28 日
matt,
how about i have Nx7 matrix?how to solve it?
Matt Fig
Matt Fig 2011 年 3 月 28 日
Put everything in a FOR loop over the rows of X. Change only the X assignments to X(ii,cnt+3) = 44 (for example), where ii is the loop index.
Amir Hamzah UTeM
Amir Hamzah UTeM 2011 年 3 月 28 日
i have tried to do this,but it all wrong. did i miss something?
X=[33 11 33 33 33 33 22
22 11 33 33 11 33 22
33 33 33 11 33 33 11];
cnt = 1;
Y = X==33;
while cnt <= length(X)-2
if Y(cnt)
if Y(cnt+1:cnt+2) % Three in a row.
for i=1:3
X(i,cnt+3) = 44;
cnt = cnt + 4;
end
elseif Y(cnt+1) % Two in a row.
for i=1:3
X(i,cnt+2) = 11;
cnt = cnt + 3;
end
else % Just one 33.
for i=1:3
X(i,cnt+1) = 22;
cnt = cnt + 2;
end
end
else
cnt = cnt + 1;
end
end
Matt Fig
Matt Fig 2011 年 3 月 28 日
I said, "Put everything in a FOR loop over the rows of X." It looks like you just put one clause of the IF statement in a FOR loop. Everything means EVERYTHING (except or course your X variable creation.)
for ii = 1:size(X,1)
cnt = 1;
Y = X(ii,:)==33;
.....
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by