Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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 Kommentar
Matt J
Matt J am 1 Feb. 2024
Bearbeitet: Matt J am 1 Feb. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 1 Feb. 2024
Bearbeitet: Matt J am 1 Feb. 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)

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by