Tensor matrix of speed

조회 수: 2 (최근 30일)
JORGE ORDOÑEZ CARRASCO
JORGE ORDOÑEZ CARRASCO 2022년 2월 15일
답변: Pavan Sahith 2023년 11월 10일
Hi everyone i got confused in tensor matrix of matlab, i have a code which calculates position and speed of 1000 particle update the position and speed, so i have a double for running one for the particles and the other for the next step, particle position and speed is updated and i need to save some positions and speed to see the this evolution. But im confuse i have this code:
As you can see bellow the matrix obtained (A(n_particle x 3 x n_steps)) the front of the cube, as i get it is read A(:,:,1) , but i need to obtain the top of this cube in the logic of matlab i know is A(1,:,:) but it only returns the first row data. I expect a tensor dimensions n_particle x 3 x n_steps for particle 1 i expect to see the evolution, what im doing wrong?
Thank you
for i=1:n_particles
for j=1:n_steps
Here i do my calculations
x_position=newposition
u_speed=newespeed
A(i,:,j)=newposition %supposely a particle i has j steps
B(i,:,j)=newspeed
end
end

답변 (1개)

Pavan Sahith
Pavan Sahith 2023년 11월 10일
Hello,
I understand you have matrix 'A' with dimensions (n_particle x 3 x n_steps) and you want to see the evolution of particle 1 so the output would be of dimensions (1 x 3 x n_steps).
When I tried with a random matrix, I can obtain the top of the matrix as a tensor.
I assume a matrix K with dimensions (2,1,3) , as you know, if you want to obtain the face of the matrix you can use K(:,:,1)
K=rand(2,3,2)
K =
K(:,:,1) = 0.7054 0.0411 0.2007 0.9059 0.6021 0.9900 K(:,:,2) = 0.3477 0.3194 0.3143 0.9252 0.1851 0.2113
K(:,:,1);
To obtain the top of the matrix as a tensor, you can use K(1,:,:)
K(1,:,:)
ans =
ans(:,:,1) = 0.7054 0.0411 0.2007 ans(:,:,2) = 0.3477 0.3194 0.3143
To make the analysis of evolution easier , you can try using the "squeeze" function to store all rows at the top of 'K' in a 2D matrix. This function returns an array with the same elements as the input array 'K', but with dimensions of length 1 removed.
m=squeeze(K(1,:,:))'
m = 2×3
0.7054 0.0411 0.2007 0.3477 0.3194 0.3143
Please refer to the MathWorks documentation to know more about
  1. "Multidimensional Arrays"-https://in.mathworks.com/help/matlab/math/multidimensional-arrays.html
  2. "squeeze"-https://in.mathworks.com/help/matlab/ref/squeeze.html
Hope that helps

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by