I need help finding the center of an n * n array. TA couldn't figure it out
112 次查看(过去 30 天)
显示 更早的评论
Assign middleElement with the element in the center of squareArray. Assume squareArray is always an n x n array, where n is odd. Hint: elementIndex should be rounded to an integer. Ex: If squareArray is [1, 2, 3; 4, 5, 6; 7, 8, 9], then middleElement is 5.
function middleElement = FindMiddle(squareArray)
% FindMiddle: Return the element in the center of squareArray
% Inputs: squareArray - n x n input array, where n is odd
%
% Outputs: selectedData - center element of squareArray
% Assign elementIndex with location of middle row/col
% Hint: Use the size() function to deterimine the dimension of squareArray
elementIndex = size(squareArray); %3.5 /2
% Assign middleElement with the center element of squareArray
middleElement = squareArray(elementIndex);
end
1 个评论
Jan
2018-1-30
@Jose Garcia: You have set a flag with the contents: "Gives the wrong answer when inputting values". It is not clear, which code you mean and what you observe. Please post a comment and add details. The flags are thought to inform admins and editors about messaged, which might violate the terms of use, e.g. if they are rude or spam.
采纳的回答
Image Analyst
2017-10-27
Like this:
m = [1, 2, 3; 4, 5, 6; 7, 8, 9]
middleElement = m(ceil(numel(m)/2))
9 个评论
Image Analyst
2020-4-26
My answer does not have the word squareArray in it, so you didn't try my answer. Again, here is the complete solution:
% Case 1. Returns 5.
m = [1, 2, 3; 4, 5, 6; 7, 8, 9]
middleValue = FindMiddle(m)
% Case 2. Returns 13.
m = [1, 2, 3, 4, 5; 6, 7, 8, 9, 10; 11, 12, 13, 14, 15; 16, 17, 18, 19, 20; 21, 22, 23, 24, 25]
middleValue = FindMiddle(m)
% Case 3. Like Case 2 but without commas. Returns 13.
m = [1 2 3 4 5;6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20;21 22 23 24 25]
middleValue = FindMiddle(m)
function middleElement = FindMiddle(m)
% Method 1:
middleIndex = ceil(size(m, 1)/2);
middleElement = m(middleIndex, middleIndex);
% Method 2: (Commented out now)
% middleElement = m(ceil(numel(m)/2))
end
If you want to replace m in FindMiddle, you can. I changed it to m since my code works for any size matrix, not just square ones.
更多回答(1 个)
Mandy Downs
2021-1-28
function middleElement = FindMiddle(squareArray)
% FindMiddle: Return the element in the center of squareArray
% Inputs: squareArray - n x n input array, where n is odd
%
% Outputs: selectedData - center element of squareArray
% Assign elementIndex with location of middle row/col
% Hint: Use the size() function to deterimine the dimension of squareArray
elementIndex = size (squareArray);
elementIndex = (elementIndex / 2) + 0.5;
% Assign middleElement with the center element of squareArray
middleElement = squareArray (elementIndex, elementIndex);
middleElement = middleElement (1)
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!