How to fill a 3D zeros matrix array

29 次查看(过去 30 天)
I have created a 3D zeros Matrix
R3_T = zeros(3,3,1442);
And want to fill it using the following computation:
R3_Thot = [cos(Tho_t), sin(Tho_t), z_0; -sin(Tho_t), cos(Tho_t), z_0; z_0, z_0, z_1];
Where:
Tho_t is an array of 1x1442
z_0 is an array of 1x1442 (all zeros)
z_1 is an array of 1x1442(all ones)
Actually I created a Matrix but was filled in the wrong way due to the fact that the elements were not in the correct order, so I got something like this:
While my R3_Thot matrix specifies another order.
Thanks.
Hugo
  1 个评论
Hugo Hernández Hernández
编辑:Hugo Hernández Hernández 2020-12-9
I am trying with the following structure:
for i=1:4326
for t=1:1442
R3_T(:,:,t) = [1,2,3;4,5,6;7,8,9];
end
end
But still in a trouble with the arrays of:
R3_Thot = [cos(Tho_t), sin(Tho_t), z_0; -sin(Tho_t), cos(Tho_t), z_0; z_0, z_0, z_1];
Because each element is an array of 1x1442 and does not fit with the for loop above, there should be a way to insert each element with its different 1442 values inside the created matrix. The above for loop is an example which I got this matrices:
Now I want to fill them with those R3_Thot data, but still do not know how.

请先登录,再进行评论。

采纳的回答

James Tursa
James Tursa 2020-12-9
编辑:James Tursa 2020-12-9
One way:
c = arrayfun(@(a)[cos(a), sin(a), 0; -sin(a), cos(a), 0; 0, 0, 1],Tho_t,'Uni',false);
result = cat(3,c{:});
Your for-loop would have worked also if you had done this by itself without the outer loop:
for t=1:numel(Tho_t)
R3_T(:,:,t) = [cos(Tho_t(t)), sin(Tho_t(t)), 0; -sin(Tho_t(t)), cos(Tho_t(t)), 0; 0, 0, 1];
end
  1 个评论
Hugo Hernández Hernández
Thank you very much James!, the first solution worked as expected but for my surprise the second one also worked, I wasn't sure if that could compute the solution that I wanted so I was trying different ways and concatenations in order to get the assignment of my data.
Thanks!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by