I have a matrix [ 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0] for exemple
I'd like to have an outing:
2 x 4 "0" in succession
2 x 2 "0" in succession
1 x 3 "0" in succession
How to program it
Thank you.

2 comentarios

Image Analyst
Image Analyst el 11 de Dic. de 2019
Editada: Image Analyst el 11 de Dic. de 2019
I have no idea what you want. What is an "outing"? Do you mean output? If so, what is the output array you want? I can't tell from your word descriptions. Type it out in numbers like you did for the input matrix.
Stephen23
Stephen23 el 12 de Dic. de 2019
stefan's "Answer" moved here:
With this matrix : [ 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 1 1 0 0 0 0 0 ]
I would have liked to have this matrix :
|5 2|
|6 1|
|4 1|
With the 1st column the number of successive 0
With the 2nd column the number of the sequence
thanks

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 12 de Dic. de 2019
Editada: Stephen23 el 12 de Dic. de 2019

0 votos

>> M = [0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,0,0];
>> D = diff([false;M(:)==0;false]);
>> L = find(D<0) - find(D>0);
>> [U,~,X] = unique(L,'stable');
>> V = histc(X,1:max(X));
>> Z = [U(:),V(:)]
Z =
5 2
6 1
4 1

7 comentarios

Stephen23
Stephen23 el 14 de Dic. de 2019
stefan's "Answer" moved here:
Thanks, it's good
I have another question:
I would like to know the number of times we have the torque (1,4) in the vector [ 0 0 0 1 1 1 2 2 2 1 1 1 1 4 4 4 4 4 1 1 4 4 4 4 4 4 2 2 4 4 4]
The answer is 2 in this case
Stephen23
Stephen23 el 14 de Dic. de 2019
"I have another question:"
What have you tried so far?
Stephen23
Stephen23 el 14 de Dic. de 2019
stefan's "Answer" moved here:
To be more precise:
I have a vector that can have values from 1 to 15.
I would like to know the number of times for all the possible changes:
for example:
from 1 to 2
from 2 to 11
from 14 to 3
....
Stephen23
Stephen23 el 14 de Dic. de 2019
Try using diff.
stefan
stefan el 14 de Dic. de 2019
Diff isn't the good fonction :
from 2 to 3 and from 3 to 4, I don't have the couples (1,2) and (2,3). I just know that there are passages from an N value to an N+1 value.
Stephen23
Stephen23 el 14 de Dic. de 2019
Editada: Stephen23 el 14 de Dic. de 2019
"I have a vector that can have values from 1 to 15. I would like to know the number of times for all the possible changes: for example:"
from 1 to 2
from 2 to 11
from 14 to 3
....
None of your provided data includes the values 11 or 14, so you are asking me to help you on an unknown algorithm with unknown data. Sorry, but my magic crystal ball is not working today.
"from 2 to 3 and from 3 to 4, I don't have the couples (1,2) and (2,3)."
Well, that makes two of us, because I don't have them either! Nor do you explain what they are, or how they relate to 1, 2, 11, 14, and 3 from your previous comment, or the binary data of your original question.
One clear explanation of the method/algorithm (complete with input and output example data) would actually mean that you get the help that you are looking.
stefan
stefan el 14 de Dic. de 2019
it's a new question.
For example with this matrix:[1 1 1 1 12 12 2 3 2 3 4 5 2 1 10 13 13 14 14 14 14 ]
in output, I would have liked to have as an output vector:
1 12 1
12 2 1
2 3 2
3 1 1
3 4 1
4 5 1
5 2 1
2 1 1
1 10 1
10 13 1
13 14 1
with :
1st column: 1st number
2nd column: 2nd number
2nd column: number of times we have the couple (1st number, 2nd number)

Iniciar sesión para comentar.

Más respuestas (1)

Akira Agata
Akira Agata el 12 de Dic. de 2019

0 votos

How about the following?
x = [0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 1 1 0 0 0 0 0];
str = num2str(x')';
c = regexp(str,'0+','match');
len = cellfun(@numel,c);
output = [(1:max(len))', accumarray(len',1)];
The result becomes like this:
>> output
output =
1 0
2 0
3 0
4 1
5 2
6 1

2 comentarios

Stephen23
Stephen23 el 12 de Dic. de 2019
stefan's "Answer" moved here:
that's not what I expect:
if I look at the matrix:
there are 2 sequences with 5 consecutive zeros
there is 1 sequence with 6 consecutive zeros
and there are 1 sequences with 4 consecutive zeros
the output matrix is:
5 2
6 1
4 1
Akira Agata
Akira Agata el 12 de Dic. de 2019
You mean, the order is also important?
My output is same as what you expected, except the order of rows and including 0 sequence cases (such as 1~3 consecutive zeros).

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 11 de Dic. de 2019

Comentada:

el 14 de Dic. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by