I have the following array of structures called test, where each field is a [1x3] structure array containing a matrix. I would like to create a new field, levelsq, which squares element-by-element each matrix. I can do this with a loop:
[test(1:3).level] = deal([1,1],[2,2],[3,3])
for i = 1:3
test(i).levelsq = test(i).level.^2
end
test.level
ans =
1 1
ans =
2 2
ans =
3 3
test.levelsq
ans =
1 1
ans =
4 4
ans =
9 9
I have got some of the way by separating and concatenating the elements, but have not yet been able to add the new field:
temp = num2cell([test.level].^2)
test.levelsq = temp{:}
??? Illegal right hand side in assignment. Too many elements.
I then tried reshaping the temp variable, but it is still not in the correct form
temp2= reshape(temp,2,3)'
temp2 =
[1] [1]
[4] [4]
[9] [9]
Is there an easier way to do this without looping or having to separate the contents? Thanks.