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

4 次查看(过去 30 天)
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]

回答(1 个)

Fangjun Jiang
Fangjun Jiang 2020-12-9
B=A([false;diff(A)>1])
  2 个评论
MNRNI
MNRNI 2020-12-9
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by