How to fill a 3D zeros matrix array

조회 수: 23 (최근 30일)
Hugo Hernández Hernández
Hugo Hernández Hernández 2020년 12월 9일
편집: James Tursa 2020년 12월 9일
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일
편집: 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
Hugo Hernández Hernández 2020년 12월 9일
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에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by