save results in new struct
Mostrar comentarios más antiguos
Hi
I want to save results of calculation in an emty struct(3*3*2 with fields:cost)named "mymax".
"stwhole" (3*3*2 with field:cost) at the following code is the name of my original struct. in each field of stwhole that is named cost are two values. and I want to multiply them (these two values of each field) to elements of matrix A separately then find the max of them and save it to corresponding fields in struct "mymax". the size of matrix A is 1*9.
this code doesn't work correctly.
PLEASE HELP ME
if true
empty_max.cost=[]
mymax=repmat(empty_max,[3,3,2])
for iteration:1:9
for k=1:9
mymax(k).cost=[]
for i=1:3
for j=1:3
for m=1:2
ops=(stwhole(i,j,m).cost).*A(K)
ty=max(ops)
mymax(k).cost=[ty]
end
end
end
end
end
9 comentarios
Sara
el 13 de En. de 2015
DO you get an error from matlab or is the result wrong?
fatema saba
el 13 de En. de 2015
Sara
el 13 de En. de 2015
On this line you define mymax as a 3,3,2 matrix
mymax=repmat(empty_max,[3,3,2])
but then you treat it as array by doing
mymax(k)
What is the desired outcome?
fatema saba
el 13 de En. de 2015
fatema saba
el 13 de En. de 2015
Editada: fatema saba
el 13 de En. de 2015
At any k-iteration you overwrite mymax though. you may want to change:
mymax(k).cost=[ty]
into
mymax(i,j,m,k).cost=ty
or
mymax(i,j,m).cost=max(ty,mymax(i,j,m).cost)
It really depends on what you're trying to achieve when you have an A matrix. Which one is the one you need? Some additional changes are needed depending on which case you meant to implement.
fatema saba
el 13 de En. de 2015
Sara
el 13 de En. de 2015
You will need to change also the initialization into
mymax=repmat(empty_max,[3,3,2,9])
and remove the line
mymax(k).cost=[]
fatema saba
el 13 de En. de 2015
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Multidimensional Arrays en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!