I need help finding the center of an n * n array. TA couldn't figure it out

조회 수: 62 (최근 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
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
Image Analyst 2017년 10월 27일
Like this:
m = [1, 2, 3; 4, 5, 6; 7, 8, 9]
middleElement = m(ceil(numel(m)/2))
  댓글 수: 9
Rik
Rik 2020년 4월 26일
That's because it isn't a function, it's a variable.
Image Analyst
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
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 CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by