two dimensional array indexing
조회 수: 5 (최근 30일)
이전 댓글 표시
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
Basil C.
2019년 10월 16일
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
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!