save results in new struct

조회 수: 1 (최근 30일)
fatema saba
fatema saba 2015년 1월 13일
댓글: fatema saba 2015년 1월 13일
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
Sara
Sara 2015년 1월 13일
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
fatema saba 2015년 1월 13일
A is 1*9 matrix I want to on each iteration one element of A multiply to stwhole(I,j,m) and max of this product save in mymax. then at the next iteration the second lement of A repeat this process and results save in mymax. and these iterations continue up to the end element of A.

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

채택된 답변

Sara
Sara 2015년 1월 13일
Here is how your code will look like:
if true
empty_max.cost=[]
mymax=repmat(empty_max,[3,3,2,numel(A)])
for k=1:numel(A)
for i=1:3
for j=1:3
for m=1:2
mymax(i,j,m,k).cost=max(stwhole(i,j,m).cost*A(k))
end
end
end
end
end
  댓글 수: 1
fatema saba
fatema saba 2015년 1월 13일
Thank you SARA. you help me a lot.
thanks for your kindness.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by