有人可以告诉我此功能的实际作用吗? [英] Can someone tell me what this function actually does?

查看:86
本文介绍了有人可以告诉我此功能的实际作用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MATLAB的新手,我想知道此函数的实际作用.该函数是从上一个问题中复制来的,该问题帮助我解决了连续查找数字频率的问题.

Hi i'm new to MATLAB and i was wondering what this function actually does. The function was copied from a previous question that helped me out with a problem to find the frequency of a number in a row.

链接: 一系列连续的数字(不同长度)

d=[3 2 4 2 2 2 3 5 1 1 2 1 2 2 2 2 2 9 2]
q = diff([0 d 0] == 2);
v = find(q == -1) - find(q == 1);

推荐答案

让我们一步一步地了解它.

Lets get to it step by step.

您有一个数组d:

d=[3 2 4 2 2 2 3 5 1 1 2 1 2 2 2 2 2 9 2]

您申请了它

q = diff([0 d 0] == 2);

这采用[0 d 0] == 2的派生词.基本上,它取所有2的数字的导数.[0 d 0] == 2的结果是:

This takes the derivative of [0 d 0] == 2. Basically it takes the derivative of all the numbers that are 2. The result of [0 d 0] == 2 is:

0 0 1 0 1 1 1 0 0 0 0 1 0 1 1 1 1 1 0 1 0

您可以看到,当原始向量中有2时,便有1;在乞gg中有0,最后有0.如果我们用q = diff([0 d 0] == 2)取它的导数:

You can see that there are 1 whenever there was a 2 in the original vector, and it has a 0 in the begging and a 0 in the end. If we take the derivative of this by q = diff([0 d 0] == 2):

q =

     0 1 -1 1 0 0  -1 0 0 0 1 -1 1 0 0 0 0 -1 1 -1

每当原始向量中出现2时,您将获得1;而当消失时,则为-1.最后一行仅分别查找1和-1,然后减去它们出现的索引,这样您现在就可以知道它们之间有多少个数字.由于1表示数字2开始",而-1表示行中没有第二个数字",这将为您提供一系列连续2的长度.

You get 1 whenever a 2 in the original vector appeared and a -1 when in disappeared. The last line finds just the 1 and just the -1 separately and substract the indices where they appeared, that way you can now how many numbers are between them. As 1 means "number 2 starts" and -1 means "no more number 2s in a row" this will give you the length of the series of 2s in a row.

v = find(q == -1) - find(q == 1);

v=
    1     3     1     5     1

开头有一个2,然后是3个2,然后是另一个,然后是5,最后是9,最后是一个.

There is a single 2 in the begining, then there is a series of 3 2s, then another single one, then 5 come and separated by a 9, the last one comes.

这篇关于有人可以告诉我此功能的实际作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆