Arrays 将较小的矩阵索引到较大矩阵的中心
我试着将以下内容作为一种爱好,而不是家庭作业。我对MATLAB是新手,对一般的编码知识有限。我有一个问题一直困扰着我。它来自于MATLAB的计算机编程:J.Michael Fitzpatrick和Akos Ledeckzi 问题10。编写一个名为Arrays 将较小的矩阵索引到较大矩阵的中心,arrays,matlab,matrix,indexing,Arrays,Matlab,Matrix,Indexing,我试着将以下内容作为一种爱好,而不是家庭作业。我对MATLAB是新手,对一般的编码知识有限。我有一个问题一直困扰着我。它来自于MATLAB的计算机编程:J.Michael Fitzpatrick和Akos Ledeckzi 问题10。编写一个名为cancel\u middle的函数,该函数将a、an-by-m 矩阵,作为输入,n和m都是奇数,k,为正 小于m和n的奇数整数(函数不必 检查输入)。函数返回输入矩阵,其中心k被k矩阵置零。查看下面的运行 >> cancel_middle(
cancel\u middle
的函数,该函数将a、an-by-m
矩阵,作为输入,n和m都是奇数,k,为正
小于m和n的奇数整数(函数不必
检查输入)。函数返回输入矩阵,其中心k被k矩阵置零。查看下面的运行
>> cancel_middle(ones(5),3)
ans =
1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1
我有一个嵌套的if函数,它可以正确地执行输入参数,但是我无法获得所需的输出。我认为这远远不够有效,我正在使用if语句,尽管这本书中没有涉及到。那是我以前的知识。因此,我认为有一个更简单、更高效的函数需要编写
function M = cancel_middle(A,k);
[m,n] = size(A);
if rem([m,n],2) == [1,1]
if rem(k,2)==1
if [k,k]<[m,n]
M = zeros(k);
else
fprintf('Error 1: k must be odd and smaller than A\n');
end
else
fprintf('Error 2: k must be odd and smaller than A\n');
end
else
fprintf('Error 3: k must be odd and smaller than A\n');
end
显然,这只是零(k)输出。您可以使用索引赋值,如下所示:
A = randi(9,5,7); % example A
k = 3; % example k
t = -(k-1)/2:(k-1)/2;
A((end+1)/2 + t, (end+1)/2 + t) = 0;
示例结果:
A =
2 5 2 9 3 6 9
6 3 0 0 0 4 8
7 7 0 0 0 8 5
6 2 0 0 0 5 6
5 7 1 5 8 4 6
您可以使用索引分配,如下所示:
A = randi(9,5,7); % example A
k = 3; % example k
t = -(k-1)/2:(k-1)/2;
A((end+1)/2 + t, (end+1)/2 + t) = 0;
示例结果:
A =
2 5 2 9 3 6 9
6 3 0 0 0 4 8
7 7 0 0 0 8 5
6 2 0 0 0 5 6
5 7 1 5 8 4 6
我不知道您是否已经介绍了MATLAB的索引部分,但如果没有,我强烈建议您仔细阅读,因为这是MATLAB的优势之一:
A=one(5,7)
和k=3
有效:
function M = cancel_middle(A,k)
% find [m,n] (the size of A)
[m,n] = size(A) ; % => m=5, n=7
% calculate the coordinates of the "center" of the matrix
center_row_idx = (m+1)/2 ; % => center_row_idx = 3
center_col_idx = (n+1)/2 ; % => center_col_idx = 4
% how much indices to take on each side of the center
half_width = (k-1)/2 ; % => center_row_idx = 3
% generate the indices which will be zeroed
row_indices = center_row_idx-half_width:center_row_idx+half_width ; % => row_indices = [2,3,4]
col_indices = center_col_idx-half_width:center_col_idx+half_width ; % => col_indices = [3,4,5]
% generate the output matrix M (as a copy of A)
M = A ;
% now you can zero the elements of matrix M directly by their indices
M(row_indices,col_indices) = 0 ;
end
您可以验证:
>> M = cancel_middle(ones(5,7),3)
M =
1 1 1 1 1 1 1
1 1 0 0 0 1 1
1 1 0 0 0 1 1
1 1 0 0 0 1 1
1 1 1 1 1 1 1
我不知道您是否已经介绍了MATLAB的索引部分,但如果没有,我强烈建议您仔细阅读,因为这是MATLAB的优势之一:
A=one(5,7)
和k=3
有效:
function M = cancel_middle(A,k)
% find [m,n] (the size of A)
[m,n] = size(A) ; % => m=5, n=7
% calculate the coordinates of the "center" of the matrix
center_row_idx = (m+1)/2 ; % => center_row_idx = 3
center_col_idx = (n+1)/2 ; % => center_col_idx = 4
% how much indices to take on each side of the center
half_width = (k-1)/2 ; % => center_row_idx = 3
% generate the indices which will be zeroed
row_indices = center_row_idx-half_width:center_row_idx+half_width ; % => row_indices = [2,3,4]
col_indices = center_col_idx-half_width:center_col_idx+half_width ; % => col_indices = [3,4,5]
% generate the output matrix M (as a copy of A)
M = A ;
% now you can zero the elements of matrix M directly by their indices
M(row_indices,col_indices) = 0 ;
end
您可以验证:
>> M = cancel_middle(ones(5,7),3)
M =
1 1 1 1 1 1 1
1 1 0 0 0 1 1
1 1 0 0 0 1 1
1 1 0 0 0 1 1
1 1 1 1 1 1 1
到目前为止,您的大多数代码似乎都在检查输入(您的问题并没有特别要求)。而不是创建一个独立的零点矩阵,考虑如何计算A的元素应该变为零并直接设置它们……@ EtMube,我运行这些检查,因此函数可以确定输入是否确实是问题所要求的。所以如果我输入一个4乘4的矩阵,我会得到一个预期的错误。我非常喜欢您将A的指定元素更改为零的方法,而不是创建单独的零矩阵。谢谢,我建议大家阅读一下,熟悉索引,因为这是解决这类问题的关键。@SardarUsama,是的!绝对是一个强大的复制品。这个任务必须是重复的……到目前为止,您的大多数代码似乎都在检查输入(您的问题并没有特别要求)。而不是创建一个独立的零点矩阵,考虑如何计算A的元素应该变为零并直接设置它们……@ EtMube,我运行这些检查,因此函数可以确定输入是否确实是问题所要求的。所以如果我输入一个4乘4的矩阵,我会得到一个预期的错误。我非常喜欢您将A的指定元素更改为零的方法,而不是创建单独的零矩阵。谢谢,我建议大家阅读一下,熟悉索引,因为这是解决这类问题的关键。@SardarUsama,是的!绝对是一个强大的复制品。这个任务一定是重复的……代码高尔夫对你影响太大了,路易斯:-)但是无论如何+1是为了一个好把戏,用
end
关键字。@Hoki-Nah。一个高尔夫版本应该是t=1-k/2:k/2;A(end/2+t,end/2+t)=0
:-)代码高尔夫对你的影响太大了,路易斯:-)但无论如何+1是为了用end
关键字玩一个漂亮的把戏。@Hoki-Nah。一个高尔夫版本应该是t=1-k/2:k/2;A(结束/2+t,结束/2+t)=0