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

Click For Summary
SUMMARY

This discussion focuses on counting the number of rows and columns in a numeric matrix using MATLAB without utilizing built-in functions like size or length. The proposed solution involves using a while loop combined with try-catch exception handling to determine the dimensions of the matrix. The user suggests initializing a counter for rows and using a dummy variable to store row data, incrementing the counter until an exception is thrown, indicating that the index exceeds the matrix dimensions. A similar approach is recommended for counting columns by adjusting the indexing method.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Familiarity with loops and control structures in programming
  • Knowledge of exception handling in MATLAB
  • Basic concepts of matrix dimensions and indexing
NEXT STEPS
  • Implement a MATLAB function to count columns using the proposed method
  • Explore MATLAB's exception handling in depth
  • Learn about MATLAB matrix manipulation techniques
  • Investigate alternative methods for matrix dimension checking without built-in functions
USEFUL FOR

Students learning MATLAB, educators designing programming assignments, and developers seeking to enhance their understanding of matrix operations in MATLAB.

ihasfigs
Messages
1
Reaction score
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
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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K