[問題] 不好意思 請問一下 已回收

看板MATLAB作者時間16年前 (2009/05/22 15:46), 編輯推噓2(203)
留言5則, 3人參與, 最新討論串1/1
不好意思,今天在寫這段時 一直沒辦法解決; 所以想請各位高手幫忙我看一下問題在哪邊 ><" function new_popu=ga_operator (init_popu,popu_fit,n,xover_rate) %selection counter=0; for z=1:n/2 parent1=0; parent2=0; new_popu=[]; x =rand(); y =rand(); check_point1 = 0 popu_fit_sum = cumsum(popu_fit) xover_point = 3; for i=1:n surv_rate = popu_fit_sum(i) / sum(popu_fit) check_point1 = x / sum(popu_fit) while check_point1 < surv_rate check_point1 = check_point1+1 end if (check_point1 < surv_rate) temp1 = init_popu(i,:) parent1=temp1 break; end end check_point1 = 0 for i=1:n surv_rate = popu_fit_sum(i) / sum(popu_fit) check_point1 = x / sum(popu_fit) while check_point1 < surv_rate check_point1 = check_point1+1 end if (check_point1 < surv_rate) temp2 = init_popu(i,:) parent2=temp2 break; end end check_point1 = 0 %crossover if rand > xover_rate new_popu(z*2-1,:)=[parent1(1:xover_point) parent2(xover_point+1:6)] new_popu(z*2,:)=[parent2(1:xover_point) parent1(xover_point+1:6)] else new_popu(z*2,:)=[parent1] new_popu(z*2-1,:)=[parent2] end end 然後執行時 跑出這段錯誤 ??? Index exceeds matrix dimensions. Error in ==> ga_operator at 65 new_popu(z*2,:)=[parent2(1:xover_point) parent1(xover_point+1:6)] 請問一下,我的語法或邏輯上哪邊出問題? 能不能給我個提示幫忙一下,感激不盡! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.126.143.29

05/22 15:52, , 1F
你一堆變數的值都沒給是要怎麼看= ="
05/22 15:52, 1F
※ 編輯: k2 來自: 140.126.143.29 (05/22 16:01)

05/22 16:03, , 2F
抱歉,第一次發問 不好意思 麻煩了 ><"
05/22 16:03, 2F

05/22 17:10, , 3F
建議你在第65行前面加秀一下 parent2 跟 parent1, 或至少
05/22 17:10, 3F

05/22 17:11, , 4F
秀一下 size(parent1) size(parent2) 與 xover_point
05/22 17:11, 4F

05/22 17:11, , 5F
若出現數值不 match 的情況你往回推大概就能自己找到錯誤
05/22 17:11, 5F
文章代碼(AID): #1A5bXG4y (MATLAB)
文章代碼(AID): #1A5bXG4y (MATLAB)