How to compare two vector with different dimension
이전 댓글 표시
Hello, I have two vectors
x1 = [1;2;4;7;8];
y1 = [2;5;8;6;1];
which are coordinates of all zero and non zero values of a matrix. And then
x2=[2;8;7]
y2=[6;1;2]
are the coordinates of just the non-zero values. Then how to get the coordinates of zero values? Please help.
댓글 수: 5
Geoff Hayes
2019년 8월 29일
Boni_PI - so if a coordinate is in (x2,y2) and in (x1,y1) then it is the coordinate of a non-zero value and so should be ignored. If the coordinate is in (x1,y1) and not in (x2,y2) then it is the coordinate of a zero value which you are interested in. Is that correct?
Star Strider
2019년 8월 29일
Something about this does not make sense. The matrix must be at least (8x8), so assuming it is just that, this:
Ln = sub2ind([8 8], x2, y2);
A = NaN(8);
A(Ln) = 42
Lz = sub2ind([8 8], x1, y1);
A(Lz) = 0
produces:
A =
NaN 0 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN 0 42 NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN 0
NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
NaN 42 NaN NaN NaN 0 NaN NaN
0 NaN NaN NaN NaN NaN NaN NaN
Boni_Pl
2019년 8월 29일
the cyclist
2019년 8월 29일
Boni_PI, I edited your question just a bit, using the CODE button to format your vectors.
Adam
2019년 8월 29일
What are the other options than 'zero or non-zero' in your matrix?! surely that set should cover every element of the matrix?
답변 (1개)
Jos (10584)
2019년 8월 29일
I assume the elements of x and y are linked? Why is the first element of (x2,y2) than not in the list of coordinaties (x1,y1)? I assume this is a mistake.
If I were you, I would link the coordinates explicitly, as rows of a N-by-2 array, allowing to use SETDIFF:
xy_all = [x1(:) y1(:)]
xy_nonzero = [x2(:) y2(:)]
xy_zero = setdiff(xy_nonzero, xy_all, 'rows')
카테고리
도움말 센터 및 File Exchange에서 Image Arithmetic에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!