![]() |
在Matlab中根据矩阵构建地图
我有一个矩阵A,其中保存有界范围(0..255)中的整数,并且我需要构建一个表,将值(0..255)映射到矩阵中所有包含此值的坐标。
实现此目标的最佳方法是什么? -我考虑过将container.Map用于任务,但Map不支持每个键使用多个值。我本可以使用列表,但这似乎效率很低,因为我必须在每次迭代中创建一个新列表。 [B]回答:[/B] 一个向量化的解决方案,其输出[URL="https://stackoverflow.com/questions/3279385/building-a-map-from-a-matrix-in-matlab/3279750#3279750"]与Mikhail的解决方案[/URL]相同,其输出是使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sort.html"]SORT[/URL]函数对图像中的所有像素值进行[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sort.html"]排序[/URL] ,使用函数[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/ind2sub.html"]IND2SUB[/URL]将[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sort.html"]SORT[/URL]返回的线性索引转换为下标索引,并将它们一起收集到使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/accumarray.html"]ACCUMARRAY[/URL]和[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/mat2cell.html"]MAT2CELL[/URL]函数的单个单元格数组: A = randi([0 255],[5 5],'uint8'); %# A sample matrix [values,indices] = sort(double(A(:))); %# Sort all the pixel values [y,x] = ind2sub(size(A),indices); %# Convert linear index to subscript counts = accumarray(values+1,1,[256 1]); %# Count number of each value map = mat2cell([yx],counts); %# Create a 256-by-1 cell array 现在,对于整数值给定iValue就可以得到N -by-2包含基质y (第一列)和x (第二列)的坐标为N与通过执行以下操作该值的图像中的像素: key = double(iValue)+1; %# Need to use double to avoid integer saturation points = map{key}; %# An N-by-2 coordinate matrix 此外,以防万一,您还可以使用函数[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/struct.html"]STRUCT[/URL]将map构造为具有字段x和y的结构数组: map = struct('x',mat2cell(x,counts),'y',mat2cell(y,counts)); 然后,您可以按如下所示访问值为iValue像素的x和y坐标: key = double(iValue)+1; x = map(key).x; y = map(key).y [url=https://stackoverflow.com/questions/3279385]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 04:51。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.