two dimensional array indexing
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Basil C.
il 16 Ott 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
Risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!