# How to find the middle element of a square array

149 ビュー (過去 30 日間)
Kevin Carty 2020 年 3 月 12 日

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)
% Assign middleElement with the center element of squareArray
middleElement = squareArray(elementIndex)
end
How do I start this? I figure I would have to set the elementIndex to an integer but I don't know how to do that. Any guidance would be appreciated.

サインインしてコメントする。

### 回答 (4 件)

Bhaskar R 2020 年 3 月 12 日

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
center_ind = (size(squareArray)+1)/2;
% Assign middleElement with the center element of squareArray
middleElement = squareArray(center_ind(1), center_ind(2))
end
##### 2 件のコメント表示非表示 1 件の古いコメント
Walter Roberson 2020 年 3 月 12 日
N is odd. If you divide it by 2 you will get a leftover 1/2, but you cannot use 1/2 as an index.
Example: n=7. 7/2=3.5 but you cannot use 3.5 as an index. What is the proper index?
1234567
!
The ! has the same number of elements before and after so the index of the center is 4.
Now take (7+1)/2 and you get 4 which is the correct index. This can also be thought of as 7/2 + 1/2 = 3.5+.5 = 4: just group the operation differently, 7/2+1/2 times 2 is 7 + 1, so divide by the 2 is (7+1)/2

サインインしてコメントする。

Jada Roth 2020 年 9 月 10 日
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 = ceil(size(squareArray)/2);
% Assign middleElement with the center element of squareArray
middleElement = squareArray(elementIndex(1),elementIndex(2));
end
##### 0 件のコメント表示非表示 -1 件の古いコメント

サインインしてコメントする。

Piyush Lakhani 2020 年 3 月 12 日
Hi Kevin,
The 'size' function returns the two element array 'row and column'. As what i can understand you want a single value that determines the variable 'n'.
so may be 'length' function gives the output you wants. Try in following way.
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 = length(squareArray)
% Assign middleElement with the center element of squareArray
middleElement = squareArray(elementIndex)
end
##### 0 件のコメント表示非表示 -1 件の古いコメント

サインインしてコメントする。

Anthony Olivares 2022 年 1 月 21 日
Here is what I did:
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
% Here I used the size() function to obtain a row vector with the
% dimensions of squareArray. Therefore, this would return [3, 3] for a
% 3x3 array.
elementIndex = (size(squareArray) + 1) / 2;
% Assign middleElement with the center element of squareArray
% Here, we simply plug in the values of the row vector obtained with
% the size() function to get the middle element in an odd 2D array.
middleElement = squareArray(elementIndex(1), elementIndex(2));
end
If we were to run FindMiddle([1, 2, 3; 4, 5, 6; 7, 8, 9]) :
• elementIndex would return [3, 3], since it is a 3x3 array.
• Therefore, squareArray(elementIndex(1), elementIndex(2)); would return a 5, since squareArray(3, 3) is 5.

サインインしてコメントする。

### カテゴリ

Find more on Matrices and Arrays in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by