two dimensional array indexing
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I am doing a homework assignment where I have to write a function that gets a randomly generated 2 dimensional array, a character (r for row and c for column), and a number of the row or column. The function then returns the row or column based off of the information inputted. It also returns an empty array when either there is a wrong input argument for the character (aka something that is not r or c) or when the row or column number doesn't exist.
The following code below is what I have done, and it passes all the tests besides checking if the expected output is obtained when all the correct information is put in. I am wondering if I can get some help on where I may have done something wrong.
function output = twoDimensionalIndexingFn(inArray, rowOrColumn, vectNumber)
newROrC = upper(rowOrColumn);
if (any(vectNumber <= 0))
output = [];
elseif (newROrC ~= 'R') || (newROrC ~= 'C')
output = [];
elseif (newROrC == 'R')
output = inArray(vectNumber, :);
elseif (newROrC == 'C')
output = inArray(:, vectNumber);
end
end
2 commentaires
Basil C.
le 16 Oct 2019
Could you elaborate on what you mean by "checking if the expected output is obtained when all the correct information is put in" because the code seems fine by me.
You could also add a condition to check that the variable vectNumber lies within the size of the inArray
if (any(inArray <= 0))
output = [];
elseif (newROrC == 'R'&& vectNumber <= size(inArray,1))
output = inArray(vectNumber, :);
elseif (newROrC == 'C'&& vectNumber <= size(inArray,2))
output = inArray(:, vectNumber);
else
output=[]
end
Réponses (0)
Voir également
Catégories
En savoir plus sur Resizing and Reshaping Matrices 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!