Effacer les filtres
Effacer les filtres

returning a unique array and comparing it with a different array.

2 vues (au cours des 30 derniers jours)
Tony Haines
Tony Haines le 31 Jan 2024
Commenté : Tony Haines le 13 Fév 2024
Hello,
I have a code that generates the y-coordinates of a few points, and returns the matrix as follows.
y_cosnode =
0.5833 0.6667 0.7500 0.8333 0.9167 1.0000
0.5000 0.5833 0.6667 0.7500 0.8333 0.9167
I wanted non-repeating values so I used y_cosnode = reshape(unique(y_cosnode),1,[]) . This gives me
y_cosnode =
0.5000 0.5833 0.6667 0.7500 0.8333 0.9167 1.0000
Now, I have another matrix array, a = [0.5000 0.5833 0.6667 0.7500 1] which I would like to compare each entry values with y_cosnode.
for i=1:size(y_cosnode,2)
[row,col] = find(y_cosnode(1,i)==a(1,:));
col
end
col =
1
col =
1x0 empty double row vector
col =
1x0 empty double row vector
col =
4
col =
1x0 empty double row vector
col =
1x0 empty double row vector
col =
5
obviously, 0.5833 and 0.6667 should not be empty because they are in matrix a. However, when I replace the matrix array 'y_cosnode', with
y_cosnode = [0.5000 0.5833 0.6667 0.7500 0.8333 0.9167 1.0000] which are the same values, just typed by myself, I get the following results. Which is what I'm looking for.
col =
1
col =
2
col =
3
col =
4
col =
1x0 empty double row vector
col =
1x0 empty double row vector
col =
5
  1 commentaire
Matt J
Matt J le 1 Fév 2024
Modifié(e) : Matt J le 1 Fév 2024
0.5833 and 0.6667 should not be empty because they are in matrix a.
They are probably not in fact in matrix a. You're only displaying these numbers to 4 decimal places, so we cannot verify that the numbers agree beyond that.

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 1 Fév 2024
Modifié(e) : Matt J le 1 Fév 2024
The whole thing can be done in one line. No need for unique, reshape, or any of the rest of it.
result = ismembertol(a,y_cosnode(:),1e-6)
And if a nonlogical indices are really needed, you can add,
find(result)
  1 commentaire
Tony Haines
Tony Haines le 13 Fév 2024
Thank you for your suggestion. All i needed was to add a tolerance value.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by