## 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 !!!

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 !!!

## RE: 4D Matrix

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"

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

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

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

=====================================

(2B)+(2B)' ?

## RE: 4D Matrix

-- 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?