Vectorize a doble loop for
Show older comments
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 Comments
Stephen23
on 2 Jun 2018
" 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
on 2 Jun 2018
Accepted Answer
More Answers (1)
Bruno Luong
on 2 Jun 2018
Edited: Bruno Luong
on 2 Jun 2018
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))
Categories
Find more on Multirate Signal Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!