Count size of matrix without size function in matlab

In summary, there are several ways to determine the size of a matrix in MATLAB without using the size function, such as using the length, numel, or ndims functions, or using the size function with different arguments. It is also possible to count the size of a matrix without using any built-in functions, but this is generally less efficient and more prone to errors. The length function returns the length of the longest dimension of a matrix, while the numel function returns the total number of elements. There are also other functions in MATLAB that can be used to count the size of a matrix, such as prod and ndims.
  • #1
hsong9
80
1
I need to keep track of the size of the matrix myself.
This means I can't use "size" function.
Is it possible?
For example,

A is a 5 x 5 matrix. I need to create a vector x=[0,0,0,0,0].
It should be just
x = zeros(size(A,1),1); %or length(A)

but I need to check the size of A matrix without size function.

Could anyone give me hints?

Thank you
 
Physics news on Phys.org
  • #2
Wouldn't A(1, :)*0 or A(:, 1)*0 work?
 
  • #3
INPUT
A= zeros(5,6) % Just as an Example
Cols=length(A) % Returns # of columns in A

OUTPUT
A= [0 0 0 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0]
Cols=6

does that help?
 

1. How can I count the size of a matrix in MATLAB without using the size function?

There are a few different ways to determine the size of a matrix in MATLAB without using the size function. One approach is to use the length function, which returns the length of the longest dimension of a matrix. Another option is to use the numel function, which returns the total number of elements in a matrix. You can also use the ndims function to get the number of dimensions in a matrix, or use the size function with the 'length' or 'numel' arguments to only return the size of a specific dimension.

2. Can I count the size of a matrix in MATLAB without using any built-in functions?

Yes, you can count the size of a matrix in MATLAB without using any built-in functions. One method is to use a for loop to iterate through each element in the matrix and keep track of the number of rows and columns. Another approach is to use the size function with the 'length' or 'numel' arguments and then subtracting 1 from the output to account for the size function itself. However, using built-in functions is generally more efficient and less prone to errors.

3. How do I count the number of rows and columns in a matrix without using the size function in MATLAB?

You can count the number of rows and columns in a matrix in MATLAB without using the size function by using the length function to get the length of the first dimension and the numel function to get the total number of elements in the matrix. Then, you can divide the total number of elements by the length of the first dimension to get the number of columns. The number of rows can be obtained by dividing the total number of elements by the number of columns.

4. What is the difference between using the length and numel functions to count the size of a matrix in MATLAB?

The length function in MATLAB returns the length of the longest dimension of a matrix, while the numel function returns the total number of elements in the matrix. So if you have a 3x5 matrix, the length function would return 5 and the numel function would return 15. Additionally, the length function can only be used on vectors, while the numel function can be used on arrays and matrices.

5. Are there any other functions in MATLAB that can be used to count the size of a matrix?

Yes, there are several other functions in MATLAB that can be used to count the size of a matrix besides the size, length, and numel functions. Some examples include the ndims function, which returns the number of dimensions in a matrix, and the prod function, which returns the product of the elements in a matrix. You can also use the size function with the 'length' or 'numel' arguments to only return the size of a specific dimension.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
850
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
116
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
Back
Top