Calculating 3D matrix
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi all, I want to calculate a 3D matrix comprising of distances from a 128 elements array(lying along x_axis) to a 2D plane of pixels with dimensions 300*200. The resultant distance matrix must have the dimensions 128*300*200. 300 is the number of pixels along z_axis and 200 are the number of pixels in x_axis. This can easily be done with 3 nested loops but I wonder if someone would please help without using any loop or with one loop at most. Appreciate your help. Thanks
1 commentaire
Rik
le 25 Oct 2017
I don't entirely understand what you mean, but I think this can be done without any loops. The solution would probably involve the meshgrid function.
Réponses (1)
Cam Salzberger
le 25 Oct 2017
It's not totally clear what is meant by "distances". Typically when you want "distances", you are doing some computation between points with at least two coordinates. On the other hand, your desired output seems to mean you want to compare the value at every point in your vector to every other point in the array, resulting in the 3-D array containing the "difference" between every possible combination.
Since R2016b (with implicit expansion), this becomes pretty trivial using reshape. Here's a quick example:
mtrx = rand(3, 2);
arr = rand(4, 1);
reshapedMtrx = reshape(mtrx, [1 size(mtrx)]);
dist = arr-reshapedMtrx;
In previous releases, you'd likely want to play with repmat to get both operands to the output array size.
-Cam
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!