- Also, please consider the fact that in your example, and in proposed vectorizations in matrix Mat columns from 4 to 10 have values equal to infinity due to maximum capacity of data precision.
how to vectorized a nested for loop?
13 次查看(过去 30 天)
显示 更早的评论
i have two for loop and i want to vectorized it . i try a lot to do it but i cannot do it. this is the simple version is let
Mat = zeros(10)
for i = 1:10
for j = 1:10
Mat(i,j) = exp((i.^5) + j.^2);
end
end
Thanks.......
0 个评论
回答(1 个)
Jan Orwat
2014-2-13
编辑:Jan Orwat
2014-2-14
Hello Mohammed, there are many ways to vectorize your loops. Check out those examples:
1.
[r,c]=meshgrid(1:10,1:10);
Mat=exp(c.^5+r.^2);
check function meshgrid in help to how it works (type doc meshgrid in commmand window)
2.
Mat=bsxfun(@(A,B)exp(A.^5+B.^2),(1:10)',1:10);
check bsxfun and function_handle
4 个评论
Jung Soo Park
2018-2-21
there are two minor errors on mohammed's reply dated 14feb2014. a=zeros(10,10) and isequal(a,Mat1)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!