How to replace an element in an struct array

20 次查看(过去 30 天)
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?

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-8-21
编辑:Azzi Abdelmalek 2013-8-21
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 个评论
tevzia
tevzia 2013-8-21
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
tevzia
tevzia 2013-8-21
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 个)

类别

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