Info
此问题已关闭。 请重新打开它进行编辑或回答。
How Can I vectorize the following codes?
1 次查看(过去 30 天)
显示 更早的评论
for i=1:40, A(i,2); end
for j=1:100, x(j)=1/j; end
s=0; for k=1:100, s=s+rand; end
I think for the second one, it should be something like
j=1:100;x(j)=1/j; ?
can anyone help me?
0 个评论
回答(1 个)
Walter Roberson
2015-9-15
The first one
for i=1:40, A(i,2); end
can be vectorized as:
;
That is, it does not do anything, unless you count the checking to be sure that A(i,2) exists.
The second one is
x = 1 ./ (1:100);
or
j = 1:100; x = 1./j;
Or if you need to be strict about the possibility that x already exists and might be larger than 100 elements,
j = 1:100; x(j) = 1./j;
The third one is
s = sum(rand(1,100));
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!