How to select specific elements of a matrix and save them in another matrix?

4 visualizaciones (últimos 30 días)
Let's say I have matrix A=[1; 2 ;3; 6; 7 ;8 ;11 ;12 ;13]
I need a script which goes through matrix A one by one and defines the elements which A(k+1)-A(k)>1
then I need that elements to be saved in matrix B
so B=[6; 11]

Respuestas (1)

Fangjun Jiang
Fangjun Jiang el 9 de Dic. de 2020
B=A([false;diff(A)>1])
  2 comentarios
MNRNI
MNRNI el 9 de Dic. de 2020
thanks.
do you know hiw we can solve it with a for loop?
when I use the following code, it saves the elements in matrix B on top of each other and matrix B shows only one element. which is B=[11]. any solution for that?
B=[];
for k=1:length(A)-1
if A(k+1)-A(k)>1
B=A(k+1);
end
end
Fangjun Jiang
Fangjun Jiang el 9 de Dic. de 2020
close enough. replace with this line
B=[B;A(k+1)];

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by