How to fill a 3D zeros matrix array

28 Ansichten (letzte 30 Tage)
Hugo Hernández Hernández
Bearbeitet: James Tursa am 9 Dez. 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 Kommentar
Hugo Hernández Hernández
Bearbeitet: Hugo Hernández Hernández am 9 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

James Tursa
James Tursa am 9 Dez. 2020
Bearbeitet: James Tursa am 9 Dez. 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 Kommentar
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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by