how to find rows and columns without using size

Introductory MATLAB course, haven't covered loops and such yet. I'm curious how to find rows + columns of an array without using size fxn? Obv numel()/length() give you rows + columns, but they don't tell you which is which. What to do next?

댓글 수: 4

Adam Danz
Adam Danz 2019년 9월 14일
편집: Adam Danz 2019년 9월 15일
Why not use the size function?
If the input is a matrix you could do this (below, but there might be better methods) but again, why?
m = rand(5,12); % demo input
m(isnan(m)|m==0) = 1; % replace NaNs & 0s with any value
sz = [mode(sum(m./m,1)), mode(sum(m./m,2))]
% or
sz = [mode(sum(m./m,1)), mode(sum(m./m,2))]
I'm not allowed to use the size fxn.
What is the actual text of the assignment, not your interpretation of it?
An assignment that asks you to find the size of a matrix without using the size function would be completely absurd. Sure you can find all sorts of roundabout ways of doing it, none of which would teach you anything useful and all of them requiring more advanced knowledge than the size function.
^^ my thoughts exactly.

댓글을 달려면 로그인하십시오.

답변 (2개)

Bruno Luong
Bruno Luong 2019년 9월 14일
편집: Bruno Luong 2019년 9월 14일

0 개 추천

for 2D array
p=numel(A);
if p==0
try
r = A(1,:);
n = 0;
m = numel(A*zeros(0,1));
catch
m = 0;
m = numel(zeros(1,0)*A);
end
else
m = numel(A(:,1));
n = p/m;
end

댓글 수: 2

unfortunately we haven't covered loops yet, is there any way to do this with basics, like using length and numel and indexing?
loop? which loop?

댓글을 달려면 로그인하십시오.

Bruno Luong
Bruno Luong 2019년 9월 15일

0 개 추천

[numel(prod(A,2)) numel(prod(A,1))] % prod instead of sum for a change

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2019년 9월 14일

댓글:

2019년 9월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by