数组和字符串方法总结

数组和字符串常用的方法-总结 控制了排序规则。该方法接受一个回调函数作为参数,该回调函数接受三个参数:当前元素的值,当前元素的索引和数组本身。在这个例子中,findIndex() 方法遍历了数组 numbers 中的所有元素,并返回第一个满足条件的元素的索引。match() 是 JavaScript 字符串对象的方法之一,用于在字符串中检索指定的值,返回找到的值并将其作为数组返回。charAt() 方法可用于获取字符串中特定位置的字符,索引应该在字符串的有效范围内,否则返回空字符串。limit:可选参数,指定返回的数组的最大长度。 阅读详情

一、数组(Array)

1、检测数组:isArray()

在ECMScript5中新增了Array.isArray()方法,用来确认某个值是不是数组。

2、转换方法:toString()、join()

调用数组的toString()方法会返回由数组中的每个值的字符串形式凭借而成的以逗号分隔的字符串。例如:
使用join()方法可以使用不同的分隔符来将数组内的值拼接成字符串的形式。join()方法接收一个参数,即用作分隔符的字符串。

var color = ['red', 'blue', 'green'];
alert(color.toString()); //red,blue,green
var color = ['red', 'blue', 'green'];
alert(color.join('|')); //red|blue|green
3、栈方法:push()、pop()

栈是一种LIFO(Last-In-First-Out,先进后出)的数据结构。
push()方法可以接收任意数量的参数,并把它们逐个添加到数组末尾,并返回修改后的数组长度。
pop()方法是从数组的末尾移除最后一项,并放回移除的项。例如:

var colors = new Array();
var count = colors.push('red', 'green');
alert(count); //2

count = colors.push('black');
alert(count); //3

var item = colors.pop();
alert(item); //"black"
alert(colors.length); //2
4、队列方法:push()、shift()

队列数据结构的访问规则的FIFO(First-In-First-Out,先进先出)。
shif()方法可以移除数组的第一项并返回该项。
同时数据还提供了一个unshift()方法,与shift()方法相反,它是在数组的前端添加人一个项并返回新数组的长度。

5、重排序方法:reverse()、sort()

reverse()方法会反转数组项的顺序。如:

var values = [1, 2, 3, 4, 5];
values.reverse();
alert(values);  //5,4,3,2,1

在默认情况下sort(),方法按升序排列数组项。sort()方法会调用每个数组项的toString()方法,然后比较得到的字符串,已确定如何排序。即使数组中的每一项都是数组,sort()方法比较的也是字符串。例如:

var values = [0, 1, 5, 10, 15];
values.sort();
alert(values);  //0,1,10,15,5

为了更精确地进行排序,sort()方法可以接收一个比较函数作为参数,比较函数可以接收两个参数,如果需要交换两个参数的位置,返回正数,不需要交换怎返回负数,两个参数相等则返回0。例如:

//升序
function compare(value1, value2) {
       if  (value1 < value2) {
              return -1;
       }  else if (value1 > value2) {
              return 1;
       } else {
              return 0;
       }
}

var values = [0, 1, 5, 10, 15];
values.sort(compare);
alert(values);  //0,1,5,10,15
6、操作方法:concat()、slice()、splice()

(1)concat()
先创建当前数组一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回一个新构建的数组。不会改变原数组的项。

var colors = ["red", "blue", "green"];
var colors2 = colors.concat("yellow", ["black", "brown"]);

alert(colors);  //red,blue,green
alert(colors2); //red,blue,green,yellow,black,brown

(2)slice()
从当前数组中截取一到多个项创建一个新数组。接收一或两个参数,即要返回项的起始和结束位置,但不包括结束位置的项。如果只有一个参数,则是从起始位置到数组末尾的所有项。

var colors = ["red", "blue", "green", "black", "brown"];
var colors2 = colors.slice(1);
var colors3 = colors.slice(1, 4);

alert(colors2);  //blue,green,black,brown
alert(colors3); //blue,green,black

如果slice()的参数中有一个是负数,则用数组的长度加上该数来确定相应的位置。如果结束位置小于起始位置,则返回空数组。

(3)splice()
splice()方法可以对数组项进行删除、替换、插入操作。
删除:需要指定2个参数,要删除的第一项的位置和要删除的项数。如splice(1, 2);
插入:需要指定3个参数,起始位置、0(要删除的项数)和要插入的项。如果要插入多个项,可以继续传入第4、第5、...、第n个项。
替换:需要指定3个参数,起始位置、要删除的项数和要插入的任意数量的项。可以向指定位置插入任意数量的项,且同时删除任意数量的项。

splice()方法始终会返回一个数组,该数组包含从原始数组中删除的项,如果没有删除的项,则返回一个空数组。

//删除
var colors = ["red", "blue", "green"];
var removed = colors.splice(0,1);
alert(colors);  //blue,green
alert(removed);  //red

//插入
removed = colors.splice(1, 0, "black", "brown");
alert(colors);  //blue,black,brown,green
alert(removed);  //[]

//替换
removed = colors.splice(1, 1, "red", "purple");
alert(colors);  //blue,red,purple,brown,green
alert(removed);  //black
7、位置方法:indexOf()、lastIndexOf()

这两个方法都可以接收两个参数:要查找的项和表示查找起点位置的索引(可选)。返回值是要查找的项在数组中的位置,如果没有找到,则返回-1。
indexOf():从前向后查找
lastIndexOf():从后向前查找

var nums = [1, 2, 3, 4, 5, 4, 3, 2, 1];

alert(nums.indexOf(4)); //3
alert(nums.lastIndexOf(4));  //5
alert(nums.indexOf(4,4));  //5
alert(nums.lastIndexOf(4,4));  //3
alert(nums.indexOf(6));   //-1
8、迭代方法:every()、some()、filter()、forEach()、map()

every()和some()都用于查询数组中的项是否满足某个条件,every()是当传入函数的每一项都符合时返回true,而some()是只要有一项满足就返回true。

var nums = [1, 2, 3, 4, 5, 4, 3, 2, 1];
var everyResult = nums.every(function(item, index, array) {
    return (item > 2);
});
alert(everyResult);  //false

var someResult = nums.some(function(item, index, array) {
    return (item > 2);
});
alert(someResult);  //true

filter()返回数组中符合条件的项的数组。

var nums = [1, 2, 3, 4, 5, 4, 3, 2, 1];
var filterResult = nums.filter(function(item, index, array) {
    return (item > 2);
});
alert(filterResult);  //[3, 4, 5, 4, 3]

map()返回在原始函数每一项上运行传入函数的结果的数组。

var nums = [1, 2, 3, 4, 5, 4, 3, 2, 1];
var mapResult = nums.map(function(item, index, array) {
    return (item * 2);
});
alert(mapResult);  //[2, 4, 6, 8,10, 8, 6, 4, 2]

forEach()对数组中的每一项运行传入的函数,没有返回值。

var nums = [1, 2, 3, 4, 5, 4, 3, 2, 1];
nums.forEach(function(item, index, array) {
    dosomething;
});
9、归并方法:reduce()、reduceRight()

迭代数组的所有项,然后构建一个返回的值。reduce()是从数组的第一项开始,逐个遍历到最后,reduceRight()则是从数组的最后一项开始,向前遍历到第一项。
这两个方法都接收两个参数,一个是在每一项上调用的函数,二是作为归并基础的初始值(可选)。传入的函数可以接收四个参数:前一个值、当前值、项的索引和数组对象。因此第一次迭代是发生在数组的第二项。

//数组中所有项的求和
var values = [1, 2, 3, 4, 5];
var sum = values.reduce(function(prev, cur, index, array) {
    return prev + cur;
});
alert(sum);  //15

二、字符串(String)

1、charAt()和charCodeAt()

都接收一个参数,即基于0 的字符位置。
charAt():以单字符字符串的形式返回给定位置的那个字符。
charCodeAt():返回给定位置字符的字符编码。

var stringValue = "Hello World";

alert(stringValue.charAt(1));  //"e"
alert(stringValue.chaCodeAt(1));  //"101"
2、concat()和加号操作符(+)

concat()用于将一个或多个字符串拼接起来,返回拼接得到的新字符串。但是实际操作中用到更多的是加号操作符(+),更加的简便易行。

3、slice()、substr()、substring()

这三个方法都会返回被操作字符串的一个子字符串。可以接收两个参数,用来表示子字符串的开始位置和结束位置。

slice()和substring()第二个参数指定的是子字符串最后一个字符后面的位置,也就是截取的子字符串中不包含该位置的字符。
substr()第二个参数指定的是子字符串的长度。

var str = "hello world";

alert(str.slice(3));  //"lo world"
alert(str.substring(3));  //"lo world"
alert(str.substr(3));  //"lo world"
alert(str.slice(3, 7));  //"lo w"
alert(str.substring(3, 7));  //"lo w"
alert(str.substr(3, 7));  //"lo worl"

如果给的参数是负数:
slice() 会将传入的负值与字符串的长度相加;
substr() 会将负的第一个参数加上字符串的长度,而将第二个负的参数转换为0;
substring() 会将所有的负值参数都转换为0。会自动将小的参数作为开始位置,大的参数作为结束位置。

var str = "hello world";

alert(str.slice(-3));  //"rld"   alert(str.slice(8))
alert(str.substring(-3));  //"hello world"   alert(str.substring(0))
alert(str.substr(-3));  //"rld"   alert(str.substr(8))
alert(str.slice(3, -4));  //"lo w"  alert(str.slice(3,8))
alert(str.substring(3, -4));  //"hel"   alert(str.substring(3,0))  alert(str.substring(0, 3))
alert(str.substr(3, -4));  //""   alert(str.substr(3, 0))
4、indexOf()和lastIndexOf()

与数组中的位置方法相同。这两个方法都是从一个字符串中搜索给定的子字符串,然后返回子字符串的位置,如果没有找到,则返回-1。

5、trim()

该方法会创建一个字符串的副本,删除前置和后缀的所有空格。如:

var str = "     hello world     ";
var trimedStr = str.trim();

alert(str);  // "     hello world     "
alert( trimedStr);  // "hello world"
6、大小写转换:toLowerCase()和toUpperCase()
var str = "hello world";
alert(str.toUpperCase());  //"HELLO WORLD"
alert(str.toLowerCase());  //"hello world"
7、模式匹配方法:match()、search()、replace()、split()

match() 与RegExp的exec()方法相同。只接收一个参数,正则表达式或者RegExp对象。

var text = "cat, bat, hat, fat";
var pattern = /.at/;

var matches = text.match(pattern);
alert(matches.index);  //0
alert(matches[0]);  //"cat"
alert(pattern.lastIndex);  //0

search() 返回字符串中第一个匹配项的索引,如果没有找到匹配项则返回-1。

var text = "cat, bat, hat, fat";
var pos = text.search(/at/);
alert(pos);  //1

replace() 接收两个参数,第一个是RegExp对象或者一个字符串,第二个参数可以是一个字符串或者函数。如果第一个参数是字符串,则只会替换第一个子字符串,要想全部替换,则只有提供一个正则表达式,并且制定全局(g)标志。

var text = "cat, bat, hat, fat";
var result = text.replace("at", "ond");
alert(result);  //"cond, bat, hat, fat"

result = text.replace(/at/g, "ond");
alert(result);  //"cond, bond, hond, fond"

replace() 方法第二个参数可以是一个函数。函数中传递三个参数:模式的匹配项(一个或者多个)、模式匹配项在字符串中的位置和原始字符串。

function htmlEscape(text) {
    return text.replace(/[<>"&]/g, function(match, pos, originText) {
        switch(match) {
            case "<":
                return "&lt";
            case ">":
                return "&gt";
            case "&":
                return "&amp;";
            case "\"":
                return "&quot";
        }
    });
}

alert(htmlEscape("<p class=\"greeting\">hello world!</p>"));  //&lt;p class=&quot;greeting&quot;&gt;hello world!&lt;/p&gt;

split() 基于制定的分隔符将一个字符串分割成多个子字符串,并将结果放在一个数组中。与数组的join()方法相反。split()可以接收可选的第二个参数,用于制定数组的大小。

python:实现矩阵SVD分解(附完整源码) 阅读详情

相关推荐

STM32驱动MG996R舵机的PWM控制与闭环原理

舵机是一种典型的模拟闭环伺服系统,其核心是通过电位器反馈实现角度位置的自动校正。工作原理基于负反馈控制:目标PWM脉宽决定期望角度,电位器输出实时反馈实际角度,控制电路计算偏差并驱动电机消除误差。该机制赋予舵机强带载能力与抗干扰性,是机器人、云台、智能执行器等场景的关键执行单元。在嵌入式开发中,STM32通过定时器生成标准20ms周期、0.5–2.5ms脉宽的PWM信号,需严格遵循电气规范,并解决PA15引脚SWJ复用冲突、定时器重映射及CCR精度计算等关键问题。本文深入解析MG996R舵机控制全流程,涵盖

weixin_42600407的博客 566

数组和字符串的常用方法总结

数组和字符串的常用方法总结

xy2041622796的博客 1519

2080Ti双卡开启NVLink

本文介绍了如何在Ubuntu 22.04系统下给2080Ti双卡开启NVLink的方法。

flymyd的博客 2907

数组、字符串的一些常用方法

1、数组的方法 shift() ;删除数组中第一个元素,并将长度减1. pop():删除数组中最后一个元素,并将长度减1. unshift():往数组前面添加一个或多个数组元素,长度要改变。如:arrObj.unshift(“a”,”b”,”c”); ...

chendu98的博客 1774

SAP成本核算变式OKKN配置详解:从CK11N到CK24,手把手教你搞定标准成本发布

本文深入解析SAP成本核算变式OKKN的配置原理与实战应用,涵盖从CK11N到CK24的标准成本发布全流程。详细讲解数量结构控制、价值结构配置及间接费用计算规则,帮助成本会计精准掌握标准成本计算逻辑,解决BOM版本选择、工艺路线获取等常见问题,提升企业成本管理效率。

weixin_27219587的博客 292

(25.08)Ubuntu20.04+ROS1复现FAST-LIO2

请确保在。

m0_69023437的博客 3996

数组和字符串常用方法

一.数组常用方法 ◉push()——末尾添加 ​ 语法:数组.push(数据) ​ 返回值:追加数据后的新数组长度 ◉pop()——末尾删除 ​ 语法:数组.pop() ​ 返回值:被删除的数据 ◉unshift()——头部添加 ​ 语法:数组.unshift(数据) ​ 返回值:添加数据后新数组的长度 ◉shift()——头部删除 ​ 语法:数组.shift() ​ 返回值:被删除的数据 ◉reveres()——数组反转 ​ 语法:数组.reveres() ​ 返回值:反转后的数组 ◉splice()

weixin_46087056的博客 395

古月学院《如何在Gazebo中实现阿克曼转向车的仿真》课程资料在ROS Noetic版本中运行时的常见错误及解决方法

   最近想在Gazebo中开发一些仿真内容,于是购买了古月学院的《如何在Gazebo中实现阿克曼转向车的仿真》课程,但是由于ROS 版本的更新,拿到手的课程资料不能直接在ROS Noetic版本中运行,经过我这几天的探索,对遇到的错误及解决方法进行了整理,方便后面购买课程的小伙伴更快的成功运行程序    在文章正式开始之前,先放一下我之前为了寻找该资料报错的原因,而写的两篇与该课程相关的文章链接,感兴趣的可以自行阅读    以阿克曼转向车为例对URDF和XACRO文件进行解读(点击可跳转)    将x

慕羽★的博客 5976

Js数组和字符串方法(API总结与应用)

match方法传入一个正则表达式作为参数,与search方法的区别是:match方法会返回结果分为两种情况:若正则表达式设为全局匹配g会返回一个匹配的子字符串结果(数组形式),若未设定全局匹配则会返回一个有特定结构的结果数组。当数组内元素的不符合条件时,map方法内部不符合条件的元素仍然会根据return的内容占据原来的位置;start是数组开始提取的位置,若是负值则代表倒数第几位开始(等价array.length - n),若start超出数组长度则会返回空数组。数组方法在JS中是一个重要的知识点。

m0_64460287的博客 777

Coze智能体开发:如何搭建一个生成长文本的工作流

扣子工作流提供了流程定制化的能力,我们可以把长文生成的流程拆分为多个步骤,例如先通过大模型节点生成大纲、再使用循环节点依次扩写各个段落,最后将各个段落组合起来,从而实现长文生成流程。此工作流可适用于学术写作、商业报告、小说创作等多种场景。

王国平的博客 1792

通信原理 | 通俗理解 | 因子图、消息传递以及它们之间的关系

在算法迭代过程中,通过反复更新这些消息,直到收敛,最终可以得到每个变量的边缘概率分布或最有可能的整体状态。算法通过因子图中的节点反复交换消息,直到找到最优解或所有消息稳定下来,从而解决了比如聚会日期这样的优化问题。在实际应用中,这种方法可以用来解决更复杂的问题,比如图像识别、语音识别和机器学习中的其他问题。,特别是在处理概率模型和执行概率推断时,因子图提供了一个强大的工具。(比如朋友的空闲时间和地点的可用性),并且你希望尽可能让所有人满意。在真正的因子图和消息传递算法中,这个过程被数学化和形式化。

若北辰 1207

字符串和数组的方法总结

字符串和数组的操作方法总结前言一、字符串的常用方法1.String对象常用方法:2.String对象常用方法代码及其输出结果:二、数组的常用方法1.数组对象常用方法:2.数组对象常用方法代码简单汇总总结 前言 提示:在前期学习和后期应用,都要频繁使用字符串和数组的方法。 例如: 前期js ,jq ,后期vue,vue尤其需要扎实的数组操作方法基础。当然面试也是经常会被问道数组方法。 提示:以下是我总经结的字符串与数组的方法,包括概念和代码展示,以及我自己认为最重要的结果演示。 一、字符串的常用方法 示

congCong的博客 887

基于特征模仿的YOLOv5中间层知识蒸馏:原理、实现与实验全解析

目标检测中的模型轻量化是一个关键挑战。本文提出一种基于特征模仿(Feature Imitation)的YOLOv5中间层知识蒸馏方法,通过让学生模型学习教师模型的中间层特征表示,显著提升轻量级检测器的性能。我们将详细阐述蒸馏损失函数的设计、特征对齐策略、训练流程,并提供完整可运行的代码实现。同时,推荐多个适合蒸馏训练的数据集,并通过实验验证方法的有效性。

YOLO 187

JavaScript数组和字符串的方法总结

参数为回调函数,会遍历数组所有的项,回调函数接受三个参数,分别为value,index,self;反向归井,同forEach,迭代数组的所有项,并构建一个最终值,由reduceRight返回。同forEach,同时回调函数返回布尔值,为true的数据组成新数组由filter返回。同forEach,同时回调函数返回布尔值,只要由一个为true,由some返回true。归并,同forEach,迭代数组的所有项,并构建一个最终值,由reduce返回。提取字符串的片断,并在新的字符串中返回被提取的部分。

weixin_67855266的博客 1326

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

数组和字符串的方法总结

.

wbuaita的博客 344

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

数组方法和字符串方法

随着es6的深入,出现了一些新的数组和字符串的方法,在这里,整理了一下一些以前的和现在的方法,希望对大家的学习有所帮助 一 数组方法: 1 join 数组拼接把数组拼接成一个字符串,参数是分隔符,参数为空时默认用逗号分隔;参数为空字符串""时没有分隔符,直接拼成字符串,此方法可以实现重复字符串 2 forEach 相当于for循环 是数组的方法 没有返回值 三个参数:数组...

weixin_43517869的博客 573

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

JavaScript中常用的数组和字符串方法总结

js数组和字符串结构+功能+用法

2301_76702774的博客 1286

数组的方法和字符串的方法

数组常用的方法有: unshift() shift()开头添加和删除 push() pop()末尾添加和删除 arr.slice(start,end) 数组的截取 arr.splice() 删除添加替换 indexof()查找数组中的元素是否存在,如果存在返回元素的下标,否则-1 concat() 连接数组 arr.includes()判断一个数组是否包含一个指定的值 如果包含返回true,否则返回false arr.join()方法,把数组按分隔符号合并成字串 arr.reverse()倒序排列数组中

Sunneyz的博客 527

JavaScript数组和字符串方法总结

JavaScript数组方法总结

weixin_43862492的博客 322

数组和字符串对象中的共有的方法

数组和字符串都有的方法: concat、includes、indexOf、lastIndexOf、slice、toString concat: 数组中concat concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。 字符串中的concat concat 方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。 concat方法并不影响原字符串。 ...

caopinpin的博客 367

数组和字符串的方法

数组方法 concat()方法用于合并两个或者多个数组,此方法不会更改现有数组,而是返回一个新数组。 includes()方法用来判断一个数组是否包含一个指定的值根据情况,如果包含返回true否组返回false indexof()方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1 lastindexof()方法返回指定元素在数组中的最后一个索引,如果不存在则返回-1。从数组的后面先前查找。 join()方法将一个数组的所有元素连接成一个字符串并返回这个字符串。如果数组只有一个

ww516629448的博客 371

js字符串和数组方法总结

字符串方法 先定义一个字符串 var str = "abbcalgldkjgldgehlgjldf" 1,indexOf(),返回指定字符创第一次出现的位置。 str.indexOf('a') //结果是0 2,lastIndexOf(),返回指定字符串最后一次出现的位置。 str.lastIndexOf('f') //结果是22 3,substring(),提取字符串中两个指定索引号之间的...

LRJ5219的博客 1024

字符串方法和数组的方法

一.字符串方法 1、charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码。  strObj.charCodeAt(index)  说明:  index将被处理字符的从零开始计数的编号。有效值为0到字符串长度减1的数字。  如果指定位置没有字符,将返回NaN。  例如:  var str = "ABC";  str.charCodeAt(0);  结果:6

梁山伯_的博客 2912

JS数组方法和字符串方法总结

数组方法 改变原数组 pop()尾删,返回删除的值,改变原数组,数组如果为空,返回undefined。 push()尾增,返回数组的长度,改变原数组。 shift()首删,返回删除的值,改变原数组。 unshift()首增,返回数组的长度,改变原数组。 splice(i,n,item) 向数组添加新项,返回删除的值,改变原数组。注:(i代表索引,n是几个,item尾新添数据。) sort()按照顺...

weixin_45575599的博客 225

总结字符串和数组的方法

1 , 字符串的方法 2 , 数组的方法

春晨秋遇的博客 289
下一篇: 前端经典面试题(一)
MaterialCoder
博客等级 码龄9年 3粉丝 21原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值