find() function indexing seems confusing

I am writing the code where I want to find the min distance from each point to cluster and the cluster they are assigned to. A step of the k-means algorithm. So I used the function of find. I am confused because even though I tried to set examples, but I keep finding myself in a loophole.
[s_norm, ind] = sort(norm_x(i,:)); % keep i th rown in ascending order
vec_distance(i) = s_norm(1); % take the smallest distance
index(i) = find(ind == 1);
Here is the example I work with
norm_x(i,:) = [2.5 1.0 0.8 3.2]
[s_norm, ind] = sort(norm_x(i,:))
s_norm = [0.8 1.0 2.5 3.2]
ind = [3 2 1 4]
As a reuslt, find(ind==1) should return "3" but why is my code not running? Please help.

6 comentarios

Walter Roberson
Walter Roberson el 22 de Sept. de 2025
Editada: Walter Roberson el 23 de Sept. de 2025
i = 1;
norm_x(i,:) = [2.5 1.0 0.8 3.2]
norm_x = 1×4
2.5000 1.0000 0.8000 3.2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[s_norm, ind] = sort(norm_x(i,:))
s_norm = 1×4
0.8000 1.0000 2.5000 3.2000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ind = 1×4
3 2 1 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
index(i) = find(ind == 1)
index = 3
Seems to be working to me?
Torsten
Torsten el 22 de Sept. de 2025
Editada: Torsten el 22 de Sept. de 2025
Just a sidenote:
i = 1;
norm_x(i,:) = [2.5 1.0 0.8 3.2];
[~,index(i)] = min(norm_x(i,:))
index = 3
is another option.
the cyclist
the cyclist el 22 de Sept. de 2025
Can you post the complete code that you think is giving an error?
I speculate that somewhere along the line you might be having a floating-point precision issue.
dpb
dpb el 22 de Sept. de 2025
"I speculate that somewhere along the line you might be having a floating-point precision issue."
It would be interesting to know precisely what @Zeyuan meant by "why is my code not running?"; we're left to speculate on the exact symptoms.
But, the find operation is for an integer, not floating point value so doesn't look as though that would be the issue, particularly with the example dataset provided.
the cyclist
the cyclist el 22 de Sept. de 2025
Ah, I only noticed the
[2.5 1.0 0.8 3.2]
and wondered about the 1.0. I had not paid sufficient attention to the sort in there.
Most likely an initialization problem before. Try one of the following:
%Example (also possible with double entries)
i=1;
norm_x(i,:) = [2.5 1.0 0.8 3.2];
% Find first smallest value + first index
[vec_distance(i), index(i)] = min(norm_x(i,:));
% Solution for an array
[vec_distance, index] = min(norm_x, [], 2);
% robust form if you you have double entries or want to find e.g. (ind == 2)
[~, ind] = sort(norm_x(i,:));
rank = zeros(1,numel(ind)); % double
rank(ind) = 1:numel(ind); % Inverse permutation
rank_of_col1 = rank(1) % should result in 3
rank_of_col1 = 3
rank_of_col2 = rank(2) % should result in 2
rank_of_col2 = 2
% Maybe you initialize "ind" incorrectly before? Try this for debugging:
[~, ind] = sort(norm_x(i,:));
class(ind) % -> 'double'
ans = 'double'
find(ind==1,1) % should result in 3
ans = 3
find(ind==2,1) % should result in 2
ans = 2

Iniciar sesión para comentar.

Respuestas (0)

Productos

Versión

R2025b

Etiquetas

Preguntada:

el 22 de Sept. de 2025

Comentada:

el 7 de Oct. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by