How to vectorize this simple for-loop
    6 次查看(过去 30 天)
  
       显示 更早的评论
    
Hello,
I'm trying to vectorize something like this:
    z=zeros(3); x=[1 2 3]; y=x;
    for i=1:length(x)  %or y, doesn't matter
    z( x(i),y(i) )=z( x(i),y(i) ) + 1;
    end
So
   z= [1 0 0 ; 0 1 0 ; 0 0 1]
What I really want is to do it for any x and y vectors in a 1000by1000 zero matrix.
I tried to do:
z(x,y)=z(x,y)+1;
But I get all values of z =1. I also tried:
z(x.,y.)=z(x.,y.)+1;
But an error message appears.
Thanks for your help.
0 个评论
更多回答(0 个)
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
			
	产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!