How to return a true/false logical array from a string array of repeating numbers?

ans = 3 2 1 5 1 4 0
I want this to return a 7x1 logical array 0 0 1 0 1 0 0. Corresponding to the repeating “1” in the ans variable. Or 1 1 0 1 0 1 1. Whichever is easier to program. How do I do this?

 Réponse acceptée

I think this may work:
A = [3 3 2 1 5 1 0 4];
[uniqueA i j] = unique(A,'first');
idRep = find(not(ismember(1:numel(A),i)))
idRep = 1×2
2 6
rep_var = A(idRep)
rep_var = 1×2
3 1
ll = ismember(A,rep_var)
ll = 1×8 logical array
1 1 0 1 0 1 0 0

Plus de réponses (1)

x = [3 2 1 5 1 4 0]
L = x == 1

5 commentaires

I should have specified that these numbers change depending on the output of other variables in my program. For example the ans variable could be “3 3 2 1 5 1 0 4” instead. Then I would want it to return “1 1 0 1 0 1 0 0”. I need a logical array that shows if there are repeats.
Sorry I misunderstood what you were looking for. I think this does what you want:
x = [3 3 2 1 5 1 0 4]
x = 1×8
3 3 2 1 5 1 0 4
u = unique(x)
u = 1×6
0 1 2 3 4 5
[N,edges,bin]= histcounts(x,[u,u(end)+1])
N = 1×6
1 2 1 2 1 1
edges = 1×7
0 1 2 3 4 5 6
bin = 1×8
4 4 3 2 6 2 1 5
N(bin)>1
ans = 1×8 logical array
1 1 0 1 0 1 0 0
Does this also do what you want?
No, I seem to be getting an array bounds error. I’m sure if I went through the troubleshooting process that it would work, but for now I will be using the other user’s answer. If I ever go back to it again then I will update this thread.
That's fine as long as you have a solution, but I'm puzzled, as to why you would have array bounds errors, when as you can see it ran without issues in the small example I show above. As you had an array bounds error, are you sure you used :
[N,edges,bin]= histcounts(x,[u,u(end)+1])
and not:
[N,edges,bin]= histcounts(x,[u,u(end+1)])

Connectez-vous pour commenter.

Catégories

En savoir plus sur Operators and Elementary Operations dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by