Thread Subject: Passing a matrix to a function

Subject: Passing a matrix to a function

From: Kingshuk Majumdar

Date: 26 May, 2008 13:30:04

Message: 1 of 3

I have two simple questions:

1. I have a matrix M(i,j). I want to create j number of
vectors V(j) each of which will have i number of entries.
For example say I have a 2 x2 matrix [1 2; 3 4]. I want to
create V(1)= (1 3), and V(2) = (2 4). How to do it?

2. How can I pass a matrix to a function? Say I have a
matrix M(i,j). I want to create a function func(M). Inside
the function I want to access the elements of matrix M.

Thank you so much in advance for your help.

Subject: Passing a matrix to a function

From: us

Date: 26 May, 2008 14:30:04

Message: 2 of 3

"Kingshuk Majumdar":
<SNIP simplicity abound...

> I have two simple questions...

one simple answer (please, don't be offended)...

peruse this great doc:

http://www.mathworks.com/access/helpdesk/help/techdoc/learn_
matlab/bqr_2pl.html

us

Subject: Passing a matrix to a function

From: OkinawaDolphin

Date: 27 May, 2008 11:41:02

Message: 3 of 3

> 1. I have a matrix M(i,j). I want to create j number of
> vectors V(j) each of which will have i number of entries.
> For example say I have a 2 x2 matrix [1 2; 3 4]. I want
to
> create V(1)= (1 3), and V(2) = (2 4). How to do it?

 M(:, 1) = [1; 3]; % first column

 M(:, 2) = [2; 4]; % second column

> 2. How can I pass a matrix to a function? Say I have a
> matrix M(i,j). I want to create a function func(M).
Inside
> the function I want to access the elements of matrix M.
>
> Thank you so much in advance for your help.

You can pass a matrix in the same way as any other kind of
parameter. A copy of the matrix is created if and only if
you write any element of the matrix. Therefore, the number
of matrix elements doesn't matter.

One example of a function that reads a matrix but does not
copy it:

 function func(M)

 SumOfElements = sum(M(:));

 IsZero = ~any(M(:));

 MaxElement = max(M(:));

 MinElement = min(M(:));

 end

The following function, however, does create an additional
copy of your matrix:

 function func(M)

 M(1, 1) = 0;

 end

Does this answer your questions?

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
reference us 26 May, 2008 10:30:07
getting started us 26 May, 2008 10:30:07
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com