Tensor matrix of speed

4 views (last 30 days)
JORGE ORDOÑEZ CARRASCO
JORGE ORDOÑEZ CARRASCO on 15 Feb 2022
Answered: Pavan Sahith on 10 Nov 2023
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

Answers (1)

Pavan Sahith
Pavan Sahith on 10 Nov 2023
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

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!