Hello, i am attempting to find repeated values in an nx1 matrix, the catch is that the unique function sorts the data. The data should not be sorted. Ideally i would like the output from my mystery function to be 1 for not repeated and 0 for repeated. Any suggestions?!
input: [1;1;1;2;3;4;5;6;6;7] output:[1;0;0;1;1;1;1;1;0;1]
the idea is that i can multiply the output and the original matrix where the data resides so that duplicate records will show as 0 values, but the first stays!
Thanks again! If you would like me to post another question please let me know!

댓글 수: 2

Image Analyst
Image Analyst 2016년 12월 19일
Give a short example of your input and desired output. Why don't you just use unique and histogram(). Are your data integers or floating point values?
William Honjas
William Honjas 2016년 12월 19일
input: [1;1;1;2;3;4;5;6;6;7] output:[1;0;0;1;1;1;1;1;0;1]
the idea is that i can multiply the output and the original matrix where the data resides so that duplicate records will show as 0 values, but the first stays! My data are floating points

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

 채택된 답변

Roger Stafford
Roger Stafford 2016년 12월 19일

2 개 추천

Let x be the given n by 1 vector.
[y,p] = sort(x);
d = diff(y)~=0;
b = [d;true] & [true;d];
b(p) = b;
Then b will be your "ideal" desired result.

댓글 수: 8

John BG
John BG 2016년 12월 19일
편집: John BG 2016년 12월 19일
Roger
your code doesn't seem to work with the following sequence
A =[ 6 -2 -9 -5 -7 -5 -1 1 -1 8 0 9 3 10 -5 4 -4 4 4 -9]
[y,p] = sort(A);
d = diff(y)~=0;
b = [d;true] & [true;d];
b(p) = b;
A =
Columns 1 through 12
6 -2 -9 -5 -7 -5 -1 1 -1 8 0 9
Columns 13 through 20
3 10 -5 4 -4 4 4 -9
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
William Honjas
William Honjas 2016년 12월 19일
Thanks also for your quick response!! I really appreciate your time and help on this question. I am relatively new to programming and am dealing with this pretty much on my own. So it means a lot that there are people out there who care! Have a great night!
@John. As William stated the problem, the given vector was n by 1, whereas in your example A is a row vector, not a column vector. For a row vector the definition of b should be altered to:
b = [d,true] & [true,d];
William Honjas
William Honjas 2016년 12월 19일
Roger,
This seems to be exactly correct for the way i described it!!! However, i described my problem incorrectly as it was very late and i was very tired.
Same idea but this is what i should have described the first time... ex:
input: [1;1;1;2;3;4;5;6;6;7] output:[1;0;0;1;1;1;1;1;0;1]
the idea is that i can multiply the output and the original matrix where the data resides so that duplicate records will show as 0 values, but the first stays!
Thanks again! If you would like me to post another question please let me know!
I assume that in giving the output as [1;0;0;1;1;1;1;1;0;1] you still want it permuted back to correspond to the order of your original input. For this revised requirement the code is nearly the same as before. Only the definition of b is changed:
[y,p] = sort(x);
b = [true;diff(y)~=0];
b(p) = b;
Jan
Jan 2016년 12월 20일
@Roger: AS far as I understand, the sorting is not wanted and "[true;diff(y)~=0]" is tzhe solution already.
Roger Stafford
Roger Stafford 2016년 12월 21일
@Jan. In William’s original request he states:, “the idea is that i can multiply the output and the original matrix where the data resides so that duplicate records will show as 0 values, but the first stays!” To be able to do such a multiplication (presumably elementwise) the final reordering by “b(p) = b” would be very necessary.
Jan
Jan 2017년 1월 16일
@Roger: I meant "The data should not be sorted." Anyway, both methods are included in your solution.

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

추가 답변 (1개)

John BG
John BG 2016년 12월 19일
편집: John BG 2016년 12월 19일

0 개 추천

but this code seems to work with any randomly generated sequence.
If it works for noise it works for any deterministic signal, oder?
N=20;
A=randi([-10 10],1,N);
% nA=[1:1:numel(A)]
A
[B,reloc]=sort(A)
% dB=diff(B)
% B(find(dB~=0))
nv=0;
k=1
while k<=numel(B)-1
k=k+1
u0=B(k-1);u1=B(k);
if u0==u1
nv=[nv k]
k=k+1
while B(k-1)==B(k);
k=k+1
end
end
end
nv(1)=[]
B(nv)
for certain sequences there is an overflow message
Index exceeds matrix dimensions.
Error in find_long_bursts (line 17)
while B(k-1)==B(k);
don't worry, sequence
nv
contains the right indices and therefore
B(bv)
is ok.
Just had to do something else, I am cleaning it if Mr Honjas likes the code :) .
if you John BG's answer useful would you please mark it as Accepted Answer?
To any other reader, please if you find this answer of any help, click on the thumbs-up vote link,
thanks in advance for time and attention
John BG

댓글 수: 1

William Honjas
William Honjas 2016년 12월 19일
Unbelievanlble how fast the community responds!! So appreciative. I am outside my office at the moment and see if the output is what I need tomorrow. I greatly appreciate the effort that you have put into this. Kinda restores my faith in humanity a bit! Thanks again!

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2016년 12월 19일

댓글:

Jan
2017년 1월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by