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

4 views (last 30 days)
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]

Answers (1)

Fangjun Jiang
Fangjun Jiang on 9 Dec 2020
B=A([false;diff(A)>1])
  2 Comments
MNRNI
MNRNI on 9 Dec 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

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!