Re: [問題] 矩陣迭代 問題已回收

看板MATLAB作者 (純粹就是鄉民)時間15年前 (2010/05/20 23:33), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《Acis (純粹就是鄉民)》之銘言: : 請教各位 : 我是要做矩陣運算的迭代 : 一開始給定M0矩陣 例:M0=[1;0;1;0;0] : (其中O I 跟x 都是矩陣) : 公式是 M1=M0+(O-I)*x1 (u取決於x1=?) : M2=M1+(O-I)*u (u取決於x2=?) : M3=M2+(O-I)*x3 (u取決於x3=?) : . : . : 我解釋一下整個程式的過程 基本上一開始已知為I,O,M0,M0C,p_color,t_color,token_color 藉由if else的判斷來決定x1=?的值之後再利用switch找到x1=?的case 再執行公式,因此輸出的結果可以知道M1跟M1C還有知道x1=?的值 目前的程式是可以正常執行一次沒有問題 但是要做多次迭代的話就陷入困境中@@ ------------------------------------------------- . function [x1,M1,M1C]=test1(M0,M0C) I=[1 0 0 0 0;0 0 0 1 0; 0 1 0 0 0;0 0 1 0 1;0 0 0 0 0]; O=[0 0 0 0 0; 1 1 0 0 0;0 0 1 0 0;0 0 0 1 0;0 0 0 0 1]; p_color=[1;3;3;3;2];t_color=[1;2;1;3;2];token_color=[1;2]; M0=[1;0;1;0;0] M0C=[1;0;2;0;0] if M0(1)==1 x1=1; elseif M0(2)==1 x1=4; elseif M0(3)==1 x1=2; elseif M0(4)==1 x1=3; elseif M0(5)==1 x1=5; end switch x1 case 1 if x1==1; %fire t1 必須 M0=[1;x;x;x;x;] if (M0(1)==1)&(M0C(1)==1)&(t_color(1)==1)&(p_color(1)==1); M1=M0+(O-I)*[1;0;0;0;0]; M1(1:end)>=0; disp('M1=');disp(M1) disp('M1 color= (1=紅色 2=綠色) ') x1=0;x2=1;x3=M0C(3);x4=M0C(4);x5=M0C(5); %[1;x;x;x;x;]→[0;1;x;x;x] M1C=[x1;x2;x3;x4;x5];disp(M1C) disp('觸發的token顏色=1=紅色') disp('firing transition=t1') else M0(1)==0; disp('無法fire t1 請重新輸入'); end end case 2 if x1==2; %fire t2 必須 M0=[x;x;1;x;x;] if (M0(3)==1)&(M0C(3)==2)&(t_color(3)>=1)&(p_color(3)>=1); M1=M0+(O-I)*[0;1;0;0;0]; M1(1:end)>=0; disp('M1=');disp(M1) disp('M1 color= (1=紅色 2=綠色) ') x1=M0C(1);x2=2;x3=0;x4=M0C(4);x5=M0C(5); %[x;x;1;x;x;]→[x;1;0;x;x] M1C=[x1;x2;x3;x4;x5];disp(M1C) disp('觸發的token顏色=2=綠色') disp('firing transition=t2') else M0(3)==0; disp('無法fire t2 請重新輸入'); end end case 3 if x1==3; %fire t3 必須 M0=[x;x;x;1;x;] if (M0(4)==1)&(M0C(4)==1)&(t_color(4)==1)&(p_color(4)>=1); M1=M0+(O-I)*[0;0;1;0;0]; M1(1:end)>=0; disp('M1=');disp(M1) disp('M1 color= (1=紅色 2=綠色) ') x1=M0C(1);x2=M0C(2);x3=1;x4=0;x5=M0C(5); %[x;x;x;1;x;]→[x;x;1;0;x] M1C=[x1;x2;x3;x4;x5];disp(M1C) disp('觸發的token顏色=1=紅色') disp('firing transition=t3') else M0(4)==0; disp('無法fire t3 請重新輸入'); end end case 4 if x1==4; %fire t4 必須 M0=[x;1;x;x;x;] if (M0(2)==1)&(t_color(2)>=1)&(p_color(2)>=1); if(M0C(2)==1) M1=M0+(O-I)*[0;0;0;1;0]; M1(1:end)>=0; disp('M1=');disp(M1) disp('M1 color= (1=紅色 2=綠色) ') x1=M0C(1);x2=0;x3=M0C(3);x4=1;x5=M0C(5); %[x;1;x;x;x;]→[x;0;x;1;x] M1C=[x1;x2;x3;x4;x5];disp(M1C) disp('觸發的token顏色=1=紅色') disp('firing transition=t4') else(M0C(2)==2) M1=M0+(O-I)*[0;0;0;1;0]; M1(1:end)>=0; disp('M1=');disp(M1) disp('M1 color= (1=紅色 2=綠色) ') x1=M0C(1);x2=2;x3=M0C(3);x4=2;x5=M0C(5); %[x;1;x;x;x;]→[x;0;x;1;x] M1C=[x1;x2;x3;x4;x5];disp(M1C) disp('觸發的token顏色=2=綠色') disp('firing transition=t4') end else M0(2)==0; disp('無法fire t4 請重新輸入'); end end case 5 if x1==5; %fire t5 必須 M0=[x;x;x;1;x;] if (M0(4)==1)&(M0C(4)==2)&(t_color(4)>=1)&(p_color(4)>=1); M1=M0+(O-I)*[0;0;0;0;1]; M1(1:end)>=0; disp('M1=');disp(M1) disp('M1 color= (1=紅色 2=綠色) ') x1=M0C(1);x2=M0C(2);x3=M0C(3);x4=0;x5=2; %[x;x;x;1;x;]→[x;x;x;0;1] M1C=[x1;x2;x3;x4;x5];disp(M1C) disp('token color=綠色') disp('firing transition=t5') else M0(4)==0; disp('無法fire t5 請重新輸入'); end end otherwise end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% disp(' ') disp('Solution=') disp('M0:');M0r=rot90(M0);disp(M0r) disp('M0C:');M0Cr=rot90(M0C);disp(M0Cr) disp('M1:');M1r=rot90(M1);disp(M1r) disp('M1C:');M1Cr=rot90(M1C);disp(M1Cr) end -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.244.180.130 ※ 編輯: Acis 來自: 60.244.180.130 (05/20 23:36)

05/21 01:03, , 1F
謝謝juzn大大的解惑 感謝 ^^
05/21 01:03, 1F
文章代碼(AID): #1BzLP0KS (MATLAB)
文章代碼(AID): #1BzLP0KS (MATLAB)