×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# 4D Matrix

## 4D Matrix

(OP)
Hi all,
I am new to matlab and working to convert a matlab code into FORTRAN code.
I have problem in understanding the following syntax in matlab,

kn=10
i=30
su=0
lam=ones(j,kn+1,i,1)
for j=1:20
su=su+lam(j,kn+1,i,1)
end

In the above code lam is a 4-D matrix with four columns of different lengths having one (1) in each element.

when this is lam is added to su, how does it work (a scalar or acts like a matrix).
if any one can explain me with an example then will be grateful !!!

Replies continue below

### RE: 4D Matrix

There's a few things I don't understand.

1 - what is the value of j when the statement "lam=ones(j,kn+1,i,1)" occurs?

2 - Can you clarify the statement "In the above code lam is a 4-D matrix with four columns of different lengths having one (1) in each element"

#### Quote:

when this is lam is added to su, how does it work (a scalar or acts like a matrix).
As written, su is a scalar which is initialized to zero.  Then an element of matrix lam is added to the scalar each time through the loop (scalar addition).  The result is summing the values of lam as its first index (j) varies from 1 to 20.

=====================================
(2B)+(2B)'  ?

### RE: 4D Matrix

(OP)
Thanks for the interest and sorry i did not mention j=20 . but to give you an idea about the lam structure following is an example.

if  lam=ones(5,3,4,2)

then it has four columns with different lengths e.g. 1st column has 5 rows, 2nd col has 3, 3rd has 4 and 4th has 2 only.

lam =
1    1     1    1

1    1     1    1

1    1     1

1          1

1

now my question is how it will pick the element of lam which would be added to su when lam(1,1,1,1) and then lam(2,1,1,1) and so on.
For matrices it is quite clear to understand as we provide only row and column and it selects the element.e.g. lam(1,2) means element in the 1st row and 2nd column.

### RE: 4D Matrix

#### Quote:

lam=ones(5,3,4,2)

then it has four columns with different lengths e.g. 1st column has 5 rows, 2nd col has 3, 3rd has 4 and 4th has 2 only.
I would have thought it would be a 4D matrix with 120 elements (120=5*3*4*2).  The "row" , "column" description generally refers to 2-d matrices.

One of us is misunderstanding something about the structure of the variable lam.

=====================================
(2B)+(2B)'  ?

### RE: 4D Matrix

I suggest you download the free matlab compatible program Octave, ff this is the first of many problems.

-- Built-in Function:  ones (X)
-- Built-in Function:  ones (N, M)
-- Built-in Function:  ones (N, M, K, ...)
-- Built-in Function:  ones (..., CLASS)
Return a matrix or N-dimensional array whose elements are all 1.
The arguments are handled the same as the arguments for eye'.

If you need to create a matrix whose values are all the same, you
should use an expression like

val_matrix = val * ones (n, m)

The optional argument CLASS, allows ones' to return an array of
the specified type, for example

val = ones (n,m, "uint8")

-- Built-in Function:  eye (..., CLASS)
Return an identity matrix.  If invoked with a single scalar
argument, eye' returns a square matrix with the dimension
specified.  If you supply two scalar arguments, eye' takes them
to be the number of rows and columns.  If given a vector with two
elements, eye' uses the values of the elements as the number of
rows and columns, respectively.  For example,

eye (3)
=>  1  0  0
0  1  0
0  0  1

The following expressions all produce the same result:

eye (2)
==
eye (2, 2)
==
eye (size ([1, 2; 3, 4])

The optional argument CLASS, allows eye' to return an array of
the specified type, like

val = zeros (n,m, "uint8")

Calling `eye' with no arguments is equivalent to calling it with
an argument of 1.  This odd definition is for compatibility with
MATLAB.

Cheers

Greg Locock

New here? Try reading these, they might help FAQ731-376: Eng-Tips.com Forum Policies  http://eng-tips.com/market.cfm?

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!