필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Tony Haines
Tony Haines 2024년 1월 31일
댓글: Tony Haines 2024년 2월 13일
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
Matt J
Matt J 2024년 2월 1일
편집: Matt J 2024년 2월 1일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2024년 2월 1일
편집: Matt J 2024년 2월 1일
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
Tony Haines
Tony Haines 2024년 2월 13일
Thank you for your suggestion. All i needed was to add a tolerance value.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by