How to replace an element in an struct array

Hi,
I have a problem about replacing an element in the array with a new one.
Could you please take a look and tell me what i'm doing wrong?
breedpop =
1x100 struct array with fields:
sched
harvvol
npv
penalty
fitness
% every array has 10 element in it.
I tried to replace one of the element in the breedpop.penalty array(doesnt matter index of the element) this way
breedpop(i).penalty = newgeneration(i).penalty;
It works if i run once but when i run it more than 100(number of iteration) bredpop.penalty becomes more than 10 element.
How can I do it?

댓글 수: 1

We would need to see more about how you create newgeneration(i).penalty

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

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 21일
편집: Azzi Abdelmalek 2013년 8월 21일

0 개 추천

Because newgeneration(i).penalty when i >100 contains more then 10 elements
If you want to take the first 10 elements
breedpop(i).penalty(1:10) = newgeneration(i).penalty(1:10);

댓글 수: 2

I wanna replace one from the list(newpopulation.penalty) to old list (breedpop.penalty)
breedpop(i).penalty = newgeneration(i).penalty(end)
however it didn't work
I solve it. thank you
breedpop(n).penalty = newpopulation(i).penalty(end)
n is the last one of the list (number of the elements)

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

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by