MATLAB 中的矩阵串联

发布于 2024-10-17 08:12:01 字数 541 浏览 3 评论 0原文

我在 Mathematica 中有这段代码:

nxBin = Table[{-5 sX + (i - 0.5)*step, nBin[[i]]}, {i, 1, Length[nBin]}]

并且我在 MATLAB 中执行了此操作:

a=zeros(length(nBin),1);
nxBin=zeros(length(nBin),1);
for i=1:length(nBin)
    anew=a*step*(i-0.5) -5*sX;
    b=zeros(length(nBin(i)),1);
nxBin(i,:)=[anew , b]
end

但是 MATLAB 说

???使用 ==> 时出错霍兹猫
CAT 参数维度不一致。

错误==>从 52 开始
nxBin(i,:)=[anew,b]

谁能告诉我为什么会出现此错误?另外,我可以用更少的行数来做到这一点吗?

I have this code in Mathematica:

nxBin = Table[{-5 sX + (i - 0.5)*step, nBin[[i]]}, {i, 1, Length[nBin]}]

and I did this in MATLAB:

a=zeros(length(nBin),1);
nxBin=zeros(length(nBin),1);
for i=1:length(nBin)
    anew=a*step*(i-0.5) -5*sX;
    b=zeros(length(nBin(i)),1);
nxBin(i,:)=[anew , b]
end

but MATLAB says

??? Error using ==> horzcat
CAT arguments dimensions are not consistent.

Error in ==> begin at 52
nxBin(i,:)=[anew , b]

Can anyone tell me why I get this error? Also, can I do this with fewer lines?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や莫失莫忘 2024-10-24 08:12:01

您想要将 n×1 数组 nBin 与步骤(可能是直方图的 x 值)连接起来。因此,您可以简单地创建“x 向量”并将它们组合起来。

nxBin = [ -5*sX + ((1:length(nBin))' - 0.5) * nStep, nBin(:)]

这是相同的分步

%# make a vector with values from 1 to nBin
x = 1:length(nBin);
%# transpose, since it's 1-by-n and we want n-by-1
x = x'; %'#
%# apply the modification to x
x = -5*sX + (x-0.5)*nStep;
%# catenate with nBin (the colon operator guarantees it's n-by-1
nxBin = [x, nBin(:)];

编辑

如果你想绘制这个,你可以这样做

plot(nxBin(:,1),nxBin(:,2),'.')

,或者,如果我猜对了,它是一个直方图

bar(nxBin(:,1),nxBin(:,2))

You want to catenate the n-by-1 array nBin with steps (probably x-values for a histogram). Thus, you can simply create the "x-vector" and combine them.

nxBin = [ -5*sX + ((1:length(nBin))' - 0.5) * nStep, nBin(:)]

Here's the same step-by-step

%# make a vector with values from 1 to nBin
x = 1:length(nBin);
%# transpose, since it's 1-by-n and we want n-by-1
x = x'; %'#
%# apply the modification to x
x = -5*sX + (x-0.5)*nStep;
%# catenate with nBin (the colon operator guarantees it's n-by-1
nxBin = [x, nBin(:)];

EDIT

In case you want to plot this, you can do

plot(nxBin(:,1),nxBin(:,2),'.')

or, if I guess right and it's a histogram

bar(nxBin(:,1),nxBin(:,2))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文