Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab:复制数组的菱形部分_Matlab_Matrix Indexing - Fatal编程技术网

Matlab:复制数组的菱形部分

Matlab:复制数组的菱形部分,matlab,matrix-indexing,Matlab,Matrix Indexing,假设数组为: A = a b c d e f g h i 我想将菱形排列,即元素d、b、f、h、e复制到新的一维数组中。上面的数组只是一个示例,矩阵可以是任意大小的矩形矩阵,菱形的位置可以是数组中的任意位置。假设您只查找5元素菱形,按照指定的顺序,您可以执行以下操作: % Previously locate the position of the middle element (e) % and store it in variable IND [i, j

假设数组为:

A =    a b c
       d e f
       g h i

我想将菱形排列,即元素d、b、f、h、e复制到新的一维数组中。上面的数组只是一个示例,矩阵可以是任意大小的矩形矩阵,菱形的位置可以是数组中的任意位置。

假设您只查找5元素菱形,按照指定的顺序,您可以执行以下操作:

% Previously locate the position of the middle element (e) 
% and store it in variable IND

[i, j] = ind2sub(size(A), IND);
J = sub2ind(size(A), i+[0 -1 0 1 0], j+[-1 0 +1 0 0]);

% Get diamond-linearized vector
dia = A(J);

如果您有图像处理工具箱,下面是一个可行的解决方案

在执行形态学操作时,可以使用菱形形状中的结构元素(选中)。现在,如果我们使用它,我们可以像这样从矩阵中提取元素。请注意,由于我使用的是单元格数组中的字符串:

clear
clc

A = {'a' 'b' 'c';'d' 'e' 'f'; 'g' 'h' 'i'}

SE = strel('diamond', 1)
Matlab告诉我们,
SE
具有以下属性(实际上它可能不是一个属性…我不知道正确的术语抱歉:p):

因此,我们可以使用STREL的
getnhood
方法作为逻辑索引,以获取一个:

NewMatrix = transpose(A(SE.getnhood()))


 NewMatrix = 

    'd'    'b'    'e'    'h'    'f'
在这里,转置用于按从上到下、从左到右的顺序获取值

因此,整个代码实际上如下所示:

A = {'a' 'b' 'c';'d' 'e' 'f'; 'g' 'h' 'i'}
DSize = 1; %// Assign size to diamond.

SE = strel('diamond', DSize)
NewMatrix = transpose(A(SE.getnhood()))
如果需要移动数组中的菱形以将其中心移到其他位置,可以在使用最后一行之前平移结构元素。函数被适当地命名为called


希望有帮助

这是通过构建具有所需菱形形状和指定中心的掩码(逻辑索引)来实现的。通过计算从每个入口到钻石中心的距离,并与适当的阈值进行比较,获得掩模:

A = rand(7,9); %// example matrix
pos_row = 3; %// row index of diamond center
pos_col = 5; %// col index of diamond center
[n_rows, n_cols] = size(A);
d = min([pos_row-1 pos_col-1 n_rows-pos_row n_cols-pos_col]); %// distance threshold 
    %// to be used for mask. Obtained by extending until some matrix border is found
ind = bsxfun(@plus, abs((1:n_rows).'-pos_row), abs((1:n_cols)-pos_col))<=d; %'// mask
result = A(ind); %// get entries defined by mask
A=rand(7,9);%//示例矩阵
位置行=3;%//钻石中心行指数
pos_col=5;%//钻石中心的col指数
[n_行,n列]=大小(A);
d=最小值([pos_row-1 pos_col-1 n_rows-pos_row n_cols-pos_col]);%//距离阈值
%//用作面具。通过延伸直到找到某个矩阵边界而获得

ind=bsxfun(@plus,abs((1:n行)。'-pos_行),abs((1:n列)-pos_列))最短还是最有效?你只是对3乘3的矩阵感兴趣,还是对任意的平方矩阵,甚至是矩形矩阵感兴趣?我假设你想把这些元素复制到一个零数组中?请编辑您的问题以澄清这些问题。是的,最有效的代码和矩阵可以是矩形的,菱形排列可以是大数组中的任意位置。我只是想通过上面的例子得到这个想法…嗨@HIMANK那么下面的答案有没有帮助你解决问题?如果是,请将其标记为已接受。谢谢
A = rand(7,9); %// example matrix
pos_row = 3; %// row index of diamond center
pos_col = 5; %// col index of diamond center
[n_rows, n_cols] = size(A);
d = min([pos_row-1 pos_col-1 n_rows-pos_row n_cols-pos_col]); %// distance threshold 
    %// to be used for mask. Obtained by extending until some matrix border is found
ind = bsxfun(@plus, abs((1:n_rows).'-pos_row), abs((1:n_cols)-pos_col))<=d; %'// mask
result = A(ind); %// get entries defined by mask