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 日
DO you get an error from matlab or is the result wrong?
fatema saba
fatema saba 2015 年 1 月 13 日
NO of course with little changes (omit iteration and also substitute A with 2)the code gives me a struct that just one filed of 18 fields is filled by result.
I can't save results in new struct. only one field is filled by result.
Sara
Sara 2015 年 1 月 13 日
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
fatema saba 2015 年 1 月 13 日
I want to any field of mymax fill with corresponding "ty" that is maximum of product of multiplying two values of each field of stwhole to element of A (or if you think simple substitute A with 2).
fatema saba
fatema saba 2015 年 1 月 13 日
編集済み: fatema saba 2015 年 1 月 13 日
for example imagine that
stwhole (1,2,1).cost= 2 3
I do 2*2=4 and 2*3=6
them max(4 and 6)is 6
then I save it to mymax(1,2,1)
or
stwhole (2,2,2).cost= 4 5
I do 2*4=8 and 2*5=10
them max(8 and 10)is 10
then I save 10 to mymax(2,2,2)
Sara
Sara 2015 年 1 月 13 日
編集済み: Sara 2015 年 1 月 13 日
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
fatema saba 2015 年 1 月 13 日
excuse me I change my code but it is something wrong yet
if true
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(i,j,m,k).cost=ty
end
end
end
end
end
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 日

0 投票

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 件)

カテゴリ

ヘルプ センター および File ExchangeMultidimensional Arrays についてさらに検索

質問済み:

2015 年 1 月 13 日

コメント済み:

2015 年 1 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by