MATLAB 中方括号的使用 [英] Usage of square brackets in MATLAB

查看:156
本文介绍了MATLAB 中方括号的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MATLAB中,您可以轻松地创建整数数组

In MATLAB you can create an array of integers easily with

N = 100; % Number of points
A = 1:N; % row vector of 1,2,3,..., 100

如果我想要列向量而不是行向量,我可以使用

If I want a column vector instead of a row vector, I can do that with

A = [1:N].';

现在,MATLAB警告我

Now, MATLAB warns me that

Use of brackets [] is unnecessary. Use parentheses to group if necessary.

嗯,它们不是不必要的,因为 1:N.'会创建行向量,因为只有标量 N 被转置,而不是整个数组.

Well, they are not unnecessary, because 1:N.' creates a row vector, as only the scalar N is transposed, as opposed to the full array.

我当然可以在该行,该文件或所有文件中禁止显示此消息,但是MATLAB为什么会首先发出此警告,因为在这种情况下,我似乎离不开这些括号?

I can of course suppress this message on that line, in that file, or in all files, but why does MATLAB throw this warning in the first place, as it seems that I can't do without those brackets in this case?

事实证明,很大一部分困惑源于The MathWorks对美式英语的使用,而其他英语国家则使用术语"括号表示() 和术语 方括号 for [].参见维基百科

It turns out a large part of the confusion stems from the usage of American English by The MathWorks, as the rest of the English-speaking world uses the term brackets for () and the term square brackets for []. See Wikipedia

推荐答案

MATLAB警告您:在必要时使用括号进行分组.在您的情况下,这是必要的.您希望.'适用于1:N,因此请使用括号().方括号 [] 用于收集其中的元素,但是已经收集了 1:N

As MATLAB warns you: Use parentheses to group if necessary. In your case it is necessary. You want .' to apply to 1:N, therefore use parentheses (). Square brackets [] are for collecting the elements within, but 1:N is already collected

A=(1:N).';

这篇关于MATLAB 中方括号的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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