开发者

Create a matrix - MATLAB

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 开发者_Python百科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


In your second example, you need to index pointers, i.e. write

pointers(i) = assign;

and not call 'length' in the second line.

However, a much easier solution is to write

pointers = (experiment(:,1).^2 + experiment(:,2).^2) <= 1

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜