小编dra*_*mnl的帖子

我应该如何在Matlab中对和为1的向量进行归一化?

我需要规范化N个整数的向量,以便:

  • 每个值与其原始值成比例(该值将介于0和1之间)
  • 所有值的总和为= 1

例如:

如果我有一个矢量

V = [2,2,1,0]
Run Code Online (Sandbox Code Playgroud)

规范化的矢量应该是:

V_norm = [0.4,0.4,0.2,0]  % 0.4+0.4+0.2 = 1
Run Code Online (Sandbox Code Playgroud)

我尝试了在这个社区和网络上找到的许多解决方案,最后我用这个代码做了:

part = norm(V);
if part > 0
  V_norm = V/part;
else % part = 0 --> avoid "divide by 0" 
  V_norm = part;
end
Run Code Online (Sandbox Code Playgroud)

这有效的问题如果:

  • 数组的所有元素都是"0" - >结果数组不会改变
  • 只有一个数组元素> 0而所有其他元素都是= 0 - >结果数组:元素> 0是1而另一个0

但如果我有不同的情况,虽然结果是成比例的,但总和不是0.例如:

   V = [1,0,1]
   V_norm = [0.74,0,0.74]

   V = [1,1,1]
   V_norm = [0.54,0.54,0.54]
Run Code Online (Sandbox Code Playgroud)

(我不确定这个数字是否正确,因为我现在不能使用Matlab,但我确定总和> 1)

Ahy暗示?

先感谢您

matlab normalize norm

9
推荐指数
3
解决办法
2万
查看次数

如何在Angular 2中导入非核心npm模块,例如(使用加密库)?

在我的Angular 2应用程序(SystemJS模块管理器,作为脚本语言的Typescript)中,我需要导入一个npm模块来处理加密(Crypto-JS; Forge-JS或任何其他服务目的)

CryptoJS的情况下,通过npm install*安装后我尝试添加:

  <script src="node_modules/crypto-js/crypto-js.js"></script>
Run Code Online (Sandbox Code Playgroud)

in index:html.

在我的服务(app/services/my-service.service.ts)我通过它导入它

  import {CryptoJS} from 'node_modules/crypto-js/crypto-js.js' // or /aes.js --> same issue
Run Code Online (Sandbox Code Playgroud)

但是,导入无法正常工作

 console.log(CryptoJS);
Run Code Online (Sandbox Code Playgroud)

打印未定义.

我还尝试添加模块路径

 System.config({
     // ...
     map: {
        CryptoJS
    }
}
Run Code Online (Sandbox Code Playgroud)

并通过我的服务导入它

 import {CryptoJS} from 'cryptoJs';
Run Code Online (Sandbox Code Playgroud)

虽然我不确定我应该在SystemJS配置中实际放入什么,但我尝试过的解决方案都没有.

编辑我也试过......

// import ... as to overcome no default export
import * as CryptoJS from 'node_modules/crypto-js/crypto-js.js';
Run Code Online (Sandbox Code Playgroud)

但是之后

 console.log(CryptoJS.); 
Run Code Online (Sandbox Code Playgroud)

不提供AES /任何方法(我的编辑通常建议我可以通过自动完成使用哪些方法)

编辑2现在感谢Thierry和PierreDuc的贡献很明显,打字和模块导入是无关联的概念.

然而,他们都没有工作.这就是我所做的:

我下载了CryptoJS打字文件,把它放在typings/cryptojs/cryptojs.d.ts中

然后我补充说

  /// …
Run Code Online (Sandbox Code Playgroud)

encryption npm angular

9
推荐指数
1
解决办法
1万
查看次数

自定义登录页面在下次身份验证中无法正确重定向

api/[...nextauth].js

export default NextAuth({
providers: [
    Providers.Credentials({
    name: 'Credentials',
    credentials: {
        email: { label: 'E-mail', type: 'text', placeholder: 'me@gmail.com' },
        password: { label: 'Password', type: 'password', placeholder: 'my password' },
    },
    async authorize(credentials, req) {
        return { email: 'x', password: 'y'}
    },
    }),
]
});
Run Code Online (Sandbox Code Playgroud)

页面/auth/signin.tsx

export default function SignIn() {
const [password, setPassword] = useState(null)
const [email, setEmail] = useState(null)

const onSubmit = () => {
    signIn("Credentials", { email, password })
}

return (
    <div>
    <label>
        Email
        <input name="email" …
Run Code Online (Sandbox Code Playgroud)

next.js next-auth

9
推荐指数
1
解决办法
2万
查看次数

如何取消订阅forkJoin返回的Observable?

在我的Angular2-typescript应用程序中,我只使用forkJoin在所有并行HTTP调用完成后返回一个Observable.

问题:订阅回调一直无限期地执行

这是我的代码:

http.service

import {Http} from "@angular/http";

constructor (private _http: HTTP) {}

makeGetRequest(....) {
    return this._http.get(URL)
           .map (res => res.json)
           .toPromise();
Run Code Online (Sandbox Code Playgroud)

my.service

import {Observable} from "rxjs/Observable";
import {HttpService} from "http.service"

constructor (private _httpService: HttpService) {}

myMethod(): Observable<any[]> {
 return Observable.forkJoin(
            this._httpService.makeGetRequest(
                URL1
            ),
            this._httpService.makeGetRequest(
                URL2
            )
        )
}
Run Code Online (Sandbox Code Playgroud)

my.component

import MyService from "my.service";
import Subscription from "rxjs";

constructor (private _service: MyService) {}

mySub: Subscription;

ngOnInit() {
    this.mySub = this._service.myMethod.subscribe(data => {
         data.forEach(console.log(data));
         this.mySub.unsubscribe();
     }
}
Run Code Online (Sandbox Code Playgroud)

我尝试过(同样的问题):

  • 在Http.service中返回一个Observable而不是Promise
  • 在my.component中使用.first().subscribe()而不是subscribe() …

fork-join rxjs angular

8
推荐指数
1
解决办法
3483
查看次数

这是扩展 eslint 规则的正确方法吗?

在我的eslint配置(YAML格式)中,我扩展了 3 种不同的配置:

extends:
- airbnb-base
- plugin:angular/johnpapa
- ionic
Run Code Online (Sandbox Code Playgroud)

我的问题如下:

  • 这是正确的格式YAML吗?
  • 其中一些扩展具有重叠规则(或其中多个扩展eslint:recommended):如果错误与这些“共享”规则之一有关,我会多次收到相同的错误吗?

yaml eslint

8
推荐指数
2
解决办法
3852
查看次数

Ionic 3中虚拟VS无限滚动的区别和用例有何不同

我已经通过Ionic 3文档,我试图了解它们之间的区别

https://ionicframework.com/docs/api/components/virtual-scroll/VirtualScroll/

https://ionicframework.com/docs/api/components/infinite-scroll/InfiniteScroll/

我看到他们使用不同的组件,虽然他们展示了InfiniteScroll的一个例子,但没有VirtualScroll的例子,它的设置也看起来比较棘手.

两者之间有什么区别,什么是可能的用例何时使用其中一个?

typescript ionic3 angular

8
推荐指数
1
解决办法
2984
查看次数

Angular-translate的localStorage:未知提供者:$ translateLocalStorageProvider

我在我的Angular(v1.x)应用程序中使用angular-translate以及使用$ translateProvider.useStaticFilesLoader从文件加载翻译

angular.module('myApp',['pascalprecht.translate'])

.config(function($translateProvider) {

     $translateProvider.useStaticFilesLoader(
          prefix: window.location.pathname.replace(/[\\\/][^\\\/]*$/, '') + '/__languages/',  // absolute path + language path
          suffix: '.json'
     });

     // $translateProvider.useLocalStorage();
}
Run Code Online (Sandbox Code Playgroud)

Angular-translate工作得很好,但在使用时:

  $translateProvider.useLocalStorage();
Run Code Online (Sandbox Code Playgroud)

(在上面的代码中评论)如本指南中提出的,我收到以下错误:

未捕获错误:[$ injector:unpr]未知提供者:$ translateLocalStorageProvider < - $ translateLocalStorage < - $ translate

当然,我包含了所有需要的js文件:

<script src="bower_components/angular-translate/angular-translate.min.js"></script>
<script src="bower_components/angular-translate-loader-url/angular-translate-loader-url.min.js"></script>
<script src="bower_components/angular-translate-loader-static-files/angular-translate-loader-static-files.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

我甚至试图:

  • 添加'angular-translate-storage-local'作为app.JS中的依赖项
  • .config块中添加$ translateUrlLoader,$ translateStaticFilesLoader.

angularjs angular-translate

7
推荐指数
1
解决办法
6880
查看次数

NSGA-2多目标遗传算法.有人能给我一个"简单的解释"吗?

我正在研究遗传算法.

有两个目标,每个目标都有自己的适应值(fv1,fv2).

我知道世代(SGE)和稳态(SS)遗传算法是如何工作的.

我试图了解NSGA-2和SPEA-2(我正在使用Java库JCLEC的实现)如何工作,特别是:

  • 什么是"外部人口",应该如何规模
  • SS和SGE单目标算法的区别是什么(每个人只有一个健身值的事实部分)

如果有人正在使用JCLEC库,这些是我设置的参数:

  • 外部人口:1000
  • k值:10
  • 其他属性与SS和SGE相同(人口规模:100,交叉:MPX交叉等...)

genetic-algorithm

6
推荐指数
2
解决办法
9362
查看次数

React教程:Uncaught TypeError:无法读取null的属性“数据”

我正在以下网址关注React教程:http//facebook.github.io/react/docs/tutorial.html

我只是 http://facebook.github.io/react/docs/tutorial.html#fetching-from-the-server 之后

我在SO上经历了类似的问题,但没有找到针对我的特定情况的解决方案。

var data = [
    {author: "Pete Hunt", text: "This is one comment"},
    {author: "Jordan Walke", text: "This is *another* comment"},
    {author: "Bob Lilly", text: "This is *another* comment 2"}

];

var Comment = React.createClass({
    render: function() {
        var rawMarkup = marked(this.props.children.toString(), {sanitize: true});
        return (
            <div className="comment">
                <h2 className="commentAuthor">
                    {this.props.author}
                </h2>
                <span dangerouslySetInnerHTML={{__html: rawMarkup}} />
            </div>
        );
    }
});

var CommentList = React.createClass({
    render: function() {
        var commentNodes = …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

5
推荐指数
1
解决办法
2万
查看次数

如何正确验证HTML输入中的数字范围?(Angular 2)

在我的Angular 2应用程序中,我有一个带有输入字段的组件,它应该接受一系列数字.

更具体地说,2例:

  • 范围0 [0] -23(小时)
  • 范围O [0] -59(分钟)

我在用

<form>
    <input type="text" pattern="[0-9]"> <!-- 0-9 -->
    <input type="text" pattern="\d|1\d|2[0-3]"> <!--  0-23 -->
    <input type="text" pattern="\d\d"> <!--  [0-99] -->
</form>
Run Code Online (Sandbox Code Playgroud)

问题是我基本上可以输入任何内容(就像忽略验证一样),包括文本.我不认为这是与Angular 2相关的问题,因为标准验证有效,例如

 <input type="number"> 
Run Code Online (Sandbox Code Playgroud)

允许只输入数字(但任何数字不是我想要的)

我还尝试使用类型编号的min = 0和max = 23(或59)属性,但这也不起作用.

regex forms html5 angular

5
推荐指数
1
解决办法
1万
查看次数