Counting the number of rows/cols in a matrix with Matlab

In summary, the conversation discusses the task of writing a function in MATLAB to count the number of rows and columns in a numeric matrix without using any built-in functions. The suggestion is to use two while loops, one for counting rows and one for counting columns. However, the challenge is to check if a specified row or column exists. The solution proposed is to use exception handling and guess a large number that is smaller than the dimensions of the array. A code is provided that uses a try-catch block to catch an exception and increment the guessed number until the exception is not thrown, thus finding the number of rows and columns in the matrix.
  • #1
ihasfigs
1
0
I'm taking a course that uses matlab, and for one assignment, we need to write a function that, among other things, counts the number of rows and columns in any given numeric matrix. The thing is that we're not allowed to use any built-in functions. No x = size(mat). No length(mat). It all has to be done with loops.
I was thinking of writing 2 while loops; one to count the number of columns and one to count the number of rows. The only problem is that I have no idea how to check whether or not a specified row or column exists. I tried the 'exist' function, but that will only tell me if the matrix exists.
any help is appreciated.
 
Physics news on Phys.org
  • #2
I don't know why schools give such things. You can do this work with exception handling, but you have to guess a large number, such that the dimensions of your array are smaller than that.

Try this code:
Code:
n=100; countr = 0; flag = 0;
while(true)
    try
        for i=1:n
            a = arr(i,:); %dummy variable storing row
            countr = countr + 1;
        end
    catch exc
            flag = 1;
    end
    if (flag == 1)
          break; %from the while loop
    end
    n = n + 100; %this statement will be reached only if the exception was not thrown
end
The logic is that if you ask for an index of the array that is greater than the dimensions of the array, MATLAB will give an exception. The catch clause will catch that exception. The flag will check whether the exception was thrown or not. If the exception was not thrown (flag = 0 after program executes), the number (n) you guessed was smaller than the size of the array, and increment that number by 100 or so, and continue the loop.

You can construct a similar program for the number of columns, replacing countr with countc. Remember to initialise countc to 0. Use a = arr(:,i) for taking in the columns.
 

What is a matrix in Matlab?

A matrix in Matlab is a two-dimensional array of numbers or variables. It is represented by rows and columns, and can be used to store and manipulate data for various calculations and analyses.

How can I count the number of rows and columns in a matrix?

To count the number of rows in a matrix, you can use the size function in Matlab. For example, size(A,1) will return the number of rows in the matrix A. To count the number of columns, you can use size(A,2).

Can I count the number of rows and columns in a matrix with different data types?

Yes, the size function in Matlab can be used to count the number of rows and columns in a matrix with different data types. It will return the total number of elements in the matrix, regardless of the data type.

Is there a way to count the number of rows and columns in a specific range of a matrix?

Yes, you can use the size function with indexing to count the number of rows and columns in a specific range of a matrix. For example, size(A(1:5, 1:3),1) will return the number of rows in the first 5 rows and first 3 columns of the matrix A.

Can I count the number of rows and columns in a matrix with missing or empty values?

Yes, the size function in Matlab will still count the number of rows and columns in a matrix with missing or empty values. However, if you want to exclude these values from the count, you can use the nnz function instead, which counts the number of non-zero elements in a matrix.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
860
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
875
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • Linear and Abstract Algebra
Replies
12
Views
1K
Back
Top