How to fill a 3D zeros matrix array

25 visualizaciones (últimos 30 días)
Hugo Hernández Hernández
Hugo Hernández Hernández el 9 de Dic. de 2020
Editada: James Tursa el 9 de Dic. de 2020
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 comentario
Hugo Hernández Hernández
Hugo Hernández Hernández el 9 de Dic. de 2020
Editada: Hugo Hernández Hernández el 9 de Dic. de 2020
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.

Iniciar sesión para comentar.

Respuesta aceptada

James Tursa
James Tursa el 9 de Dic. de 2020
Editada: James Tursa el 9 de Dic. de 2020
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 comentario
Hugo Hernández Hernández
Hugo Hernández Hernández el 9 de Dic. de 2020
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!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by