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

65 visualizaciones (últimos 30 días)
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 comentario
Jan
Jan el 30 de En. de 2018
@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.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 27 de Oct. de 2017
Like this:
m = [1, 2, 3; 4, 5, 6; 7, 8, 9]
middleElement = m(ceil(numel(m)/2))
  9 comentarios
Rik
Rik el 26 de Abr. de 2020
That's because it isn't a function, it's a variable.
Image Analyst
Image Analyst el 26 de Abr. de 2020
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.

Iniciar sesión para comentar.

Más respuestas (1)

Mandy Downs
Mandy Downs el 28 de En. de 2021
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

Categorías

Más información sobre Historical Contests en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by