创建矩阵 - MATLAB
我从 Mathematica 中得到了这个,我想在 MATLAB
pointers =
Table[If[experiment[[i, 1]]^2 + experiment[[i, 2]]^2 > 1, 0, 1], {i,
1, npoints}];
对于 n 个点,输出例如为 {0, 1, 1, 1, 1, 1, 0, 0, 1, 1} = 10.
我试过了,但错误! (我现在正在学习 MATLAB,不过我从 Mathematica 学到了一些)
assign=experiment(i,1)^2 +experiment(i,2)^2;
if assign>1
assign=0;
else assign=1;
end
pointers=assign(1:npoints);
我也这样做了,它给出了输出 1,但它是错误的:
for i=1:npoints
assign=length(experiment(i,1)^2 +experiment(i,2)^2);
if assign>1
assign=0;
else assign=1;
end
end
pointers=assign
I have this from Mathematica and I want to create it in MATLAB
pointers =
Table[If[experiment[[i, 1]]^2 + experiment[[i, 2]]^2 > 1, 0, 1], {i,
1, npoints}];
The output is for example {0, 1, 1, 1, 1, 1, 0, 0, 1, 1}, for npoints = 10.
I tried this, but it's wrong! (I am learning MATLAB now, I have a little from Mathematica though)
assign=experiment(i,1)^2 +experiment(i,2)^2;
if assign>1
assign=0;
else assign=1;
end
pointers=assign(1:npoints);
I also did this which gives output 1, but it's wrong:
for i=1:npoints
assign=length(experiment(i,1)^2 +experiment(i,2)^2);
if assign>1
assign=0;
else assign=1;
end
end
pointers=assign
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第二个示例中,您需要索引
指针
,即在第二行中写入而不是调用“length”。
然而,一个更简单的解决方案是编写
这样,您将在括号内创建一个包含平方和结果的新数组。然后可以检查该数组是否小于或等于 1(即,如果大于 1,则结果为 0),返回数组
pointers
中所有比较的结果。In your second example, you need to index
pointers
, i.e. writeand not call 'length' in the second line.
However, a much easier solution is to write
With this, you're creating, inside the parentheses, a new array with the result of the sum of squares. This array can then be checked for being smaller or equal 1 (i.e. if it's bigger than 1, the result is 0), returning the result of all the comparison in the array
pointers
.