Logical Indexing with zero and one. Getting only the change from 0 to 1 and from 1 to 0.

Is there any way to from matrix A to matrix B without using find?
A= B=
0 0
0 0
0 0
0 0
0 0
0 0
1 1
1 0
1 0
1 0
1 1
0 0
0 0
0 0
0 0
0 0
Thank you.

Respuestas (2)

Andrei Bobrov
Andrei Bobrov el 12 de Jun. de 2013
Editada: Andrei Bobrov el 12 de Jun. de 2013
B = [0;diff(A)==1] + flipud([0;diff(A(end:-1:1))==1]);
B = [false;diff(A)==1] | flipud([false;diff(A(end:-1:1))==1]); % logical
The correct should be like this. After playing around a bit I found this.
%%Example
a=0:0.01:0.12;
b=0.13:-0.01:0.03;
c=0.04:0.01:0.13;
d=0.12:-0.01:0.02;
mat=vertcat(a',b',c',d');
mat(:,2)=mat(:,1)>0.04;
mat(:,3)=[0; diff(mat(:,2))];
mat(:,4)=mat(:,3)==1;
mat(:,5)=[diff(mat(:,2)); 0];
mat(:,6)=mat(:,5)==-1;
mat(:,7)=mat(:,6)|mat(:,4);
So in this way you can index the boundaries. I would like also other people's opinion. If such a way is efficient or not. is it better to index them with find? thank you

1 comentario

I have no idea what this is about. This code has no relation to your original question whatsoever. But if it does what you want, go for it. No one cares about efficiency when you're only dealing with 315 elements. What would you save - a nanosecond? Now if you had 315 million elements, then it would be a concern.

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 12 de Jun. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by