I need help finding the center of an n * n array. TA couldn't figure it out
63 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Jan
le 30 Jan 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.
Réponse acceptée
Image Analyst
le 27 Oct 2017
Like this:
m = [1, 2, 3; 4, 5, 6; 7, 8, 9]
middleElement = m(ceil(numel(m)/2))
9 commentaires
Image Analyst
le 26 Avr 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.
Plus de réponses (1)
Mandy Downs
le 28 Jan 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
Voir également
Catégories
En savoir plus sur Historical Contests dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!