convert image into 3XN matrix
    8 ビュー (過去 30 日間)
  
       古いコメントを表示
    
I have a dem data(an image of 6000X5200). I need to convert the data and create a 3XN matrix with row number, coloum number and Z value (pixel value of the image represent the Z value).
    orgim = imread('dem');   % read DEM data
    nentry = 0;
        for ii = 1:size(orgim,2)
          for jj = 1:size(orgim,1)
                nentry = nentry +1;
                mydata(nentry, :) = [ii, jj, orgim(jj,ii)];
                clear X Y Z;
        end
      end
This works but takes time. Is there any optimum way to convert the multi-dimentional 6000X 5200 matrix as 3XN matrix, without loop
2 件のコメント
  Andrei Bobrov
      
      
 2012 年 9 月 14 日
				[x,y] = ndgrid(1:size(orgim,1),1:size(orgim,2));
mydata = [y(:),x(:),orgim(:)];
採用された回答
  Andrei Bobrov
      
      
 2012 年 9 月 14 日
        
      編集済み: Andrei Bobrov
      
      
 2012 年 9 月 14 日
  
      [x,y] = ndgrid(1:size(orgim,1),1:size(orgim,2));
mydata = [y(:),x(:),orgim(:)];
or
mydata = [fliplr(fullfact(size(orgim))), orgim(:)];
0 件のコメント
その他の回答 (2 件)
  Jan
      
      
 2012 年 9 月 14 日
        Although Andrei's solution is smarter, I suggest the pre-allocation as general programming schema:
 orgim = imread('dem');   % read DEM data
 mydata = zeros(numel(orgim), 3));  % Pre-allocate!!!
 nentry = 0;
 for ii = 1:size(orgim,2)
   for jj = 1:size(orgim,1)
     nentry = nentry +1;
     mydata(nentry, :) = [ii, jj, orgim(jj,ii)];
   end
 end
This can be noticably faster already.
0 件のコメント
参考
カテゴリ
				Help Center および File Exchange で Convert Image Type についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


