- "Multidimensional Arrays"-https://in.mathworks.com/help/matlab/math/multidimensional-arrays.html
- "squeeze"-https://in.mathworks.com/help/matlab/ref/squeeze.html
Tensor matrix of speed
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (1)
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(:,:,1);
To obtain the top of the matrix as a tensor, you can use K(1,:,:)
K(1,:,:)
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,:,:))'
Please refer to the MathWorks documentation to know more about
Hope that helps
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!