Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29253 Discussions

A question about [] in array indexing

junziyang
Beginner
814 Views

REAL*8n2mid(1,NZ-1)
REAL*8n0mid(3,NZ-1)

n2mid=n0mid([1],:)!No Problem


n2mid=n0mid(1,:)!The compiler issues the following error message:

error#6366:Theshapesofthearrayexpressionsdonotconform.[N2MID]
n2mid=n0mid(1,:)

--------^

My question: What's the difference of this two kind of array indexing? When and How to use the square bracket [] correctlly?

0 Kudos
1 Solution
Jugoslav_Dujic
Valued Contributor II
814 Views
Quoting - junziyang

REAL*8n2mid(1,NZ-1)
REAL*8n0mid(3,NZ-1)

n2mid=n0mid([1],:)!No Problem


n2mid=n0mid(1,:)!The compiler issues the following error message:

error#6366:Theshapesofthearrayexpressionsdonotconform.[N2MID]
n2mid=n0mid(1,:)

--------^

My question: What's the difference of this two kind of array indexing? When and How to use the square bracket [] correctlly?

[1], traditionally spelled (/1/), is an array constructor. The resulting array has rank 1 and has 1 element.

Thus, n0mid([1], :) is a rank-2 array, whose shape is [1, NZ-1].

1 is a scalar, thus n0mid(1, :) is a rank-1 array, whose shape is [NZ-1].

You may assign a rank-2 array to another rank-2 array (provided that the shapes are identical, which is the case). However, you may not assign a rank-1 array to a rank-2 array -- that doesn't have mathematical sense.

Rule of thumb: every scalar in an array-indexing expression reduces the rank of the resulting array for 1. For example, if A is an array, A(1) has rank 1-1=0=scalar, while A(1:2) or A([1,2]) is still a rank-1 array.

View solution in original post

0 Kudos
3 Replies
Jugoslav_Dujic
Valued Contributor II
815 Views
Quoting - junziyang

REAL*8n2mid(1,NZ-1)
REAL*8n0mid(3,NZ-1)

n2mid=n0mid([1],:)!No Problem


n2mid=n0mid(1,:)!The compiler issues the following error message:

error#6366:Theshapesofthearrayexpressionsdonotconform.[N2MID]
n2mid=n0mid(1,:)

--------^

My question: What's the difference of this two kind of array indexing? When and How to use the square bracket [] correctlly?

[1], traditionally spelled (/1/), is an array constructor. The resulting array has rank 1 and has 1 element.

Thus, n0mid([1], :) is a rank-2 array, whose shape is [1, NZ-1].

1 is a scalar, thus n0mid(1, :) is a rank-1 array, whose shape is [NZ-1].

You may assign a rank-2 array to another rank-2 array (provided that the shapes are identical, which is the case). However, you may not assign a rank-1 array to a rank-2 array -- that doesn't have mathematical sense.

Rule of thumb: every scalar in an array-indexing expression reduces the rank of the resulting array for 1. For example, if A is an array, A(1) has rank 1-1=0=scalar, while A(1:2) or A([1,2]) is still a rank-1 array.

0 Kudos
junziyang
Beginner
814 Views
Quoting - Jugoslav Dujic

[1], traditionally spelled (/1/), is an array constructor. The resulting array has rank 1 and has 1 element.

Thus, n0mid([1], :) is a rank-2 array, whose shape is [1, NZ-1].

1 is a scalar, thus n0mid(1, :) is a rank-1 array, whose shape is [NZ-1].

You may assign a rank-2 array to another rank-2 array (provided that the shapes are identical, which is the case). However, you may not assign a rank-1 array to a rank-2 array -- that doesn't have mathematical sense.

Rule of thumb: every scalar in an array-indexing expression reduces the rank of the resulting array for 1. For example, if A is an array, A(1) has rank 1-1=0=scalar, while A(1:2) or A([1,2]) is still a rank-1 array.


Thank you very much!

0 Kudos
onkelhotte
New Contributor II
814 Views
Quoting - junziyang

REAL*8n2mid(1,NZ-1)
REAL*8n0mid(3,NZ-1)

n2mid=n0mid([1],:)!No Problem


n2mid=n0mid(1,:)!The compiler issues the following error message:

error#6366:Theshapesofthearrayexpressionsdonotconform.[N2MID]
n2mid=n0mid(1,:)

--------^

My question: What's the difference of this two kind of array indexing? When and How to use the square bracket [] correctlly?


Why dont you declare

real*8 n2mid(nz-1)

Then, n2mid = n0mid(1,:) would be possible. An array of size 1 doesnt make sense to me... Why do you use it?

Markus

0 Kudos
Reply