Borrar filtros
Borrar filtros

Searching to substrat 2 matrix to get a 3 dimensional matrix

1 visualización (últimos 30 días)
Paul
Paul el 12 de Mzo. de 2024
Respondida: Shubham el 20 de Mzo. de 2024
Hi, in order to optimize my k-means algorithm, I'm looking to calculate distances without using a for loop. To achieve this, I need to subtract two matrices with the following dimensions:
- Image Matrix, size (N*5), where N is the number of pixels.
- Centers Matrix, size (5*M), where M is the number of centers.
My objective is to obtain a matrix of dimensions (N*5*M), where in the last dimension, there is the subtraction between all the pixels and the center k. Why? Because afterward, I would like to obtain the index of the minimum distance to associate each pixel with a center.
Thanks for all the help you provide!
Here the matrix :
mat = rand(N,5);
matCenters = rand(5,nbrsuperPixel);

Respuestas (1)

Shubham
Shubham el 20 de Mzo. de 2024
Hi Paul,
To achieve the subtraction between your two matrices without using explicit for-loops and to handle the broadcasting in such a way that you get an (N*5*M) result, you can use MATLAB's bsxfun function or implicit expansion. However, note that the resultant matrix size (N*5*M) as mentioned might not directly correspond to how you're planning to calculate distances, especially for k-means where you typically compute the Euclidean distance and end up with an (N*M) matrix, where each element represents the distance of a point to a center.
First, let's define your matrices:
N = 1000; % Example number of pixels
M = 3; % Example number of centers
% Example matrices
mat = rand(N,5);
matCenters = rand(5,M);
To perform the subtraction without using a for-loop and get to an (N*5*M) structure, you'll have to reshape or expand your matrices accordingly. However, directly achieving (N*5*M) for subtraction might not be straightforward due to the nature of operations you're looking for. Typically, for distance calculations in k-means, you'd expand both matrices to allow for element-wise subtraction and then compute the distance, not necessarily keeping the 5-dimensional feature space expanded across M.
Here's how you might approach it considering you want to compute distances eventually:
% Expand mat to (N*5*M)
matExp = repmat(mat, 1, 1, M);
% Reshape matCenters for subtraction, resulting in (1*5*M)
matCentersReshaped = reshape(matCenters, 1, 5, M);
% Now subtract using bsxfun (for pre-R2016b) or implicit expansion (R2016b onwards)
% Using implicit expansion:
diff = matExp - matCentersReshaped;
% Now, if you're computing Euclidean distance, you'd square, sum across the second dimension, and then take the square root
distances = sqrt(sum(diff.^2, 2));
% distances is now (N*M*1), where each slice along the third dimension is redundant. You can squeeze it to get (N*M)
distances = squeeze(distances);
% Find the index of the minimum distance for each pixel
[~, minIndex] = min(distances, [], 2);
% minIndex now contains the index of the nearest center for each pixel
This approach gives you the indices of the closest center for each pixel, which is typically what you're after in a k-means clustering scenario. Remember, the direct (N*5*M) structure might not be as useful for distance calculations as you might think, since the actual operation you're interested in is the computation of distances and finding the nearest center, which is more naturally expressed in an (N*M) structure after summing squared differences across the feature dimensions.

Categorías

Más información sobre Statistics and Machine Learning Toolbox en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by