Vectorize a doble loop for
显示 更早的评论
Hi everyboy
I am trying to vectorize the following code, but I can't. I want to calculate the matrix p_hat from the matrixs p and omega (these last matrix are of the same order) . The vector a is a function that gives me another index.
Could anyone help me, please?
for k=1:length(a)
for j=1:length(a)
if omega(j,k)==1
p_hat(k,a(j))=p(j,k);
end
end
end
2 个评论
Stephen23
2018-6-2
" The vector a is a function that gives me another index"
It can't be both: is it a vector, or a function?
jose ramirez alvarez
2018-6-2
采纳的回答
更多回答(1 个)
Bruno Luong
2018-6-2
编辑:Bruno Luong
2018-6-2
p = rand(3,4)
a = ceil(10*rand(size(p,1),1))
omega = rand(size(p))>0.4
[j,k] = find(omega)
p_hat = accumarray([k,a(j)],p(omega))
类别
在 帮助中心 和 File Exchange 中查找有关 Multirate Signal Processing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!