Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7669969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:42:55+00:00 2026-05-31T15:42:55+00:00

In Fortran, is it possible to select certain parts of the an array by

  • 0

In Fortran, is it possible to select certain parts of the an array by using some vector of logical values instead of the indices? For example like this:

iszero(1) = 0
iszero(2) = 1
iszero(3) = 0
sum0 = sum(iszero) !was  sum0 = sum(iszero==0)
!mymatrix is arbitary is 3 times 3 array
mysubmatrix(1:sum0,1:sum0) = mymatrix(iszero==0,iszero==0)
call dtrmv('l','n','u',sum0,mysubmatrix(1:sum0,1:sum0),sum0,x(1:sum0)),1)

If this is not possible directly, is there an easy (fast) way to find the indices where iszero=0?

edit: I changed the example to present a more realistic case, in previous case I just changed some values to 100.0d0, where the elementwise handling would have been ok..

edit2: had one type on the fourth row of the code

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:42:57+00:00Added an answer on May 31, 2026 at 3:42 pm
    where (mymatrix==0) mymatrix = 100.0d0
    

    will set all elements in mymatrix which are 0 to 100. If you are actually trying to do something a little more sophisticated than that, perhaps setting a matrix to have a ‘checkerboard’ of 1s and 0s you could try something like;

    mymatrix(1:m:2,2:n:2) = 100d0
    

    where m,n are the numbers of rows and columns in mymatrix. I haven’t tested this latter snippet, it’s just a suggestion to consider array subscript triplets sometimes.

    EDIT

    If you actually want to use one matrix (or vector) as the mask in the where statement and another in the assignment part, ie something like this:

    where(index_matrix==0) mymatrix = 100d0
    

    then you have (I think) to ensure that index_matrix has the same size as mymatrix. In your case you might end up with a statement such as:

    where(reshape([0,1],[3,3],pad=[0,1])==0) mymatrix = 100d0
    

    again I haven’t tested this and I don’t expect that I’ve got the padded reshape quite right but you can probably figure out the details.

    FURTHER EDIT

    I’m now finding it difficult to follow the question. The statement

    sum0 = sum(iszero==0)
    

    will assign the value 1 to sum, so the statement

    mysubmatrix(1:sum0,1:sum0) = mymatrix(iszero==0,iszero==0)
    

    will, at run time, be something like this:

    mysubmatrix(1:1,1:1) = mymatrix(iszero==0,iszero==0)
    

    and I’m not sure that the rhs and lhs conform properly for Fortran. Does this compile ? If it does, does it execute correctly (with array-bounds checking) ?

    Are you trying to create submatrix which holds only the 0 elements of mymatrix ? If so then I think you are going to have a hard time of it, for the general case. Unless you can define the locations of the elements you want to select in terms of either indices or a vector of subscripts or a subscript triplet, then I don’t see that you can make an array on the lhs from the array on the rhs.

    If the locations of the 0s are arbitrary then you might be able to do what you want by flattening the original array to rank-1 and creating a rank-1 subarray of that, but you would lose the correspondence between 2D locations and their 1D counterparts.

    FINALLY

    Don’t forget that you can, in Fortran 2003, use a pointer to refer to a sub-matrix defined by vector or triplet subscripts, for example

    pointer_to_array => target_array(1:10:2,2:10:2)
    

    and then pass pointer_to_array around.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is it possible some way to print in python in a fortran like way
I had some fortran code that looked something like: subroutine foo(mx,my,mz) real pts(3,mx,my,mz) end
I am using gfortran to compile FORTRAN 77 and would like to have DEBUG
I would like to call some functions from a Fortran shared library in Python.
Is it possible in modern Fortran to use a vector to index a multidimensional
I have some old fortran 77 I'm trying to port to gcc on Windows.
I have Fortran-based 3D column-major order array flattened into linear memory. What formula/algorithm can
Is it possible to pass fortran 77 function as a callback function pointer to
I'm controlling a creaky old FORTRAN simulator from a VB.NET GUI, using redirected I/O
I allocated value to status array like this : status[i] += 1; and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.