Is it possible to perform array operations on rows of a matrix in Matlab?

In summary, Array operations in Matlab allow you to treat each row of a matrix as a vector, and to take the square roots of each row.
  • #1
ichbinschneller
2
0
Array operations in Matlab?

Is it possible to apply operations to the rows of a matrix as an array? Such as I have a matix A which is 30 rows by 1 column and I want to treat each row as a vector such that I can solve for the polynomial roots of each row using the roots comand (treating each row as its own equation and the matrix A a system of equations)?

Also would it be possible to take the square roots of each row of a same size matrix. Meaning say I had the same size matrix and I want to take the square root of each row and have the output be a matrix of the same size just with the values of those square roots?
 
Physics news on Phys.org
  • #2
Hi, Iamfaster :wink:

Welcome to PF!

If you need to perform a function on each row of a matrix like

function(A), where A is a 30x1 vector, I think you'll have to break it up and write a for loop

Code:
for i=[1:length(A)]
     function(A(i));
end

or if A is 30x2 (or more)
Code:
for i=[1:length(A)]
     function(A(i,:));
end

if you want to take the square root, you can find it using the .^ operator, which raises each element to a power as opposed to the ^ which performs matrix multiplication

Code:
A.^(1/2)
 
  • #3
Thanks, I am getting closer now but I still can't seem to enter the code in the right form.
Say I have a matrix

g1=
5 9 8
4 7 6
1 2 8

and I want a matrix g2= roots of each row as if each row was a vector consisting of the coeficients of a polynomial. I have tried using code such as

for i=[1:length(g1)]
g2(i)=roots(g1(i));
end

but I keep getting errors. What am I doing wrong? What code would yield the results I desrire?
 
  • #4
ichbinschneller said:
Code:
for i=[1:length(g1)]
     g2(i)=roots(g1(i));
end

What am I doing wrong? What code would yield the results I desrire?

You're not indexing the entire row.

g1(1) doesn't have any meaning for a m by n matrix when n>1.

If you want to reference a single term, you need to put in the entire address lke so:

g1(1,2)

to reference the first row, second column.

If you want to reference the entire row, you need to put in the : operator like so:

g1(1,:)

Just using : by itself is "shorthand" for explicitly defining the starting and ending conditions.

If your matrix g is 4x4

Code:
g=[1,2,3,4
     6,7,8,9
    10,11,12,13
    14,15,16,17]

typing

a=g(2,2:3) will give the result a=[7,8]
b=g(:,1) will give the result b=[1;6;10;14]
c=g(3,1:4) will give the result c=[10,11,12,13]
and
d=g(3,:) will give the same result as c. d=[10,11,12,13]

Hope that helps,
 
  • #5
also, lose the [ and the ]
like this:

for i = 1:length(g1)
g2(i, :) = roots(g1(i, :));
end
 
  • #6
gerben said:
also, lose the [ and the ]
like this:

for i = 1:length(g1)
g2(i, :) = roots(g1(i, :));
end

Works with or without. I usually leave them in.
 
  • #7
yes it works because [1:5] evaluates to: 1, 2, 3, 4, 5 (you could say: for i = [1,4,18])
but you can tell the loop that you want "it" done for the numbers 1 through 5 instead of telling it that you want "it" done for the numbers 1, 2, 3, 4 and 5.

(I guess you leave them in because of similarity with other programming languages that usually have brackets there, like in C/C++: for(i=1; i<5; i++) )
 

1. What is an array in Matlab?

An array in Matlab is a collection of elements with the same data type, organized in a grid of rows and columns. It can be a single-dimensional, two-dimensional, or multi-dimensional structure that can store and manipulate data efficiently.

2. How do I create an array in Matlab?

To create an array in Matlab, you can use the built-in functions like zeros, ones, or rand. For example, A = zeros(3,4) creates a 3x4 array with all elements initialized to 0.

3. What are some common array operations in Matlab?

Some common array operations in Matlab include accessing array elements using indexing, performing arithmetic operations on arrays, and using built-in functions like mean, max, and min to calculate statistical measures.

4. How do I perform element-wise operations on arrays in Matlab?

To perform element-wise operations on arrays in Matlab, you can use the .* operator. For example, A .* B will multiply each element in array A with the corresponding element in array B.

5. Can arrays of different sizes be operated on in Matlab?

Yes, in Matlab, arrays of different sizes can be operated on as long as the dimensions are compatible. For example, a 3x4 array can be added to a 3x1 array, but not to a 2x4 array as the number of rows must match.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Precalculus Mathematics Homework Help
Replies
32
Views
818
  • Linear and Abstract Algebra
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
873
  • Linear and Abstract Algebra
Replies
2
Views
916
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top