for loop - how can I find the place of some numbers in a Matlab matrix? -
i have matrix values -180 180 , want find places each value , save them in new matrix, how can it?
i want create loop goes values -180:1:180
, finds locations(i,j)
each value. e.g want find locations value -180
, save them in new matrix. possible done? wrote here code loop function , works don't know how save values know location each value.
for a= 180:-1:-180 [i,j]=find(orient==a) end
so many ways this.assuming orient matrix, use cells store i's , j's, like:
orient=round(180.*(2.*rand(100,100)-1)); %or whatever find_results=cell(361,2); a=-180:1:180; index=1:length(a) [i,j]=find(orient==a(index)); find_results{index,1}=i; find_results{index,2}=j; end
not elegant or efficient, works fine me. know wantted use matrix, cant freely choose length of each column in matrix, think do. otherwhise go like
find_results(index,1:length(i))=i;
but need first create matrix end filled zeros,because way work make sure size(find_result,1)=number of elements in matrix(cells dont care). use sparse matrix man, getting out of hand.
--------------------------edit---------------------------------------
i guess matrix, size huge:
orient=round(180.*(2.*rand(100,100)-1)); %or whatever a=-180:1:180; find_results_i=nan(size(orient,1).*size(orient,2),size(a,2)); find_results_j=nan(size(orient,1).*size(orient,2),size(a,2)); index=1:length(a) [i,j]=find(orient==a(index)); find_results_i(1:length(j),index)=i; find_results_j(1:length(j),index)=j; end
if familiar sparse, use
find_results_i=sparse(size(orient,1).*size(orient,2),size(a,2)); find_results_j=sparse(size(orient,1).*size(orient,2),size(a,2));
to save ton of memory, altougth slower
Comments
Post a Comment