How to do this efficiently?
이전 댓글 표시
Hello all,
I have this variable a[k,m]=max(k*Ts+taup,m*Ts+tauq) for k,m=0,1,...,N-1. I want to find the matrix A where [A]_{k,m}=a[k,m] efficiently. To do so, I define two matrices RowInc and ColInc as
RowInc =(0:N-1)'*ones(1,N);
ColInc =transpose(RowInc);
Then I write
for pp=1:Np
for qq=1:Np
A=max(RowInc*Ts+tau(pp),ColInc*Ts+tau(qq));
end
end
Does this give me what I want?
Thanks
댓글 수: 4
Image Analyst
2014년 8월 9일
Please explain in words what this non-standard MATLAB syntax is supposed to do: [A]_{k,m}=a[k,m]
First of all, MATLAB uses parentheses, not brackets for indexing. It uses brackets but not in the way you used them. Also the underline is not right, nor are your braces and brackets on the left hand side. I have no idea what you intend.
S. David
2014년 8월 9일
Image Analyst
2014년 8월 9일
Nothing is in gray. But basically you mean that A=a everywhere. The A and a matrices are identical.
S. David
2014년 8월 9일
답변 (2개)
dpb
2014년 8월 9일
...I want to find the matrix A where [A]{k,m}=a[k,m]..._
A=max(RowInc*Ts+tau(pp),ColInc*Ts+tau(qq));
will end up w/ just a single value for A at the last loop of pp and qq since it overwrites the previous A each iteration. But, I don't believe it does what you want, anyway.
Should be simply
A(A==a);
if I understand the query correctly.
댓글 수: 5
S. David
2014년 8월 9일
dpb
2014년 8월 9일
Again, not sure what the question is (or if there even is one)?
Does, or does not, the a array already exist? If so, why is not A result
A(A==a);
as above? Unless you're after a full MxN array only partly populated but if so you've not specified what the remaining elements should be. Presuming zeros,
A=a;
A(A~=a)=0;
Image Analyst
2014년 8월 9일
His latest comment just said that all elements of A equal "a", so
A(A==a);
net, does absolutely nothing.
S. David
2014년 8월 9일
dpb
2014년 8월 9일
IA, I couldn't figure out what his last comment said (and little of the rest) if after "I have this variable a[k,m]..." there isn't an array a.
Guess I'll leave the field bloodied on this one...
dpb
2014년 8월 10일
OK, from the loop solution one can write
[x,y]=ndgrid(0:N-1,0:N-1);
A=max(Ts.*x+taup,Ts.*y+tauq);
trading memory for the loop. The loop solution could be simplified since there's only a dependence upon kk for the one term and mm for the other, they could be precomputed outside the loops. Preallocating also would help, of course. Not sure how the timings would come out in the end.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!