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 9206965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:17:18+00:00 2026-06-18T00:17:18+00:00

I build a 2-dimensional array (a matrix) consisting of a vector of vectors: (setq

  • 0

I build a 2-dimensional array (a matrix) consisting of a vector of vectors:

(setq zero-row [0 0 0 0 0])
  => [0 0 0 0 0]

(setq zero-mat (make-vector 4 zero-row))
  => [[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]]

I’ll set the element in row 2, column 3 (0-indexed) to 42 by replacing row 2 with a vector containing the changed element:

(aset zero-mat 2 [0 0 0 42 0])
  => [0 0 0 42 0]

zero-mat
  => [[0 0 0 0 0] [0 0 0 0 0] [0 0 0 42 0] [0 0 0 0 0]]

It works.

Next I try to build a function which takes this approach to set the (i,j)-th element in such a 2-dimensional array:

(defun matrix-set (mat i j elt)
"Set the (i, j)-th element of mat to elt. mat is a vector of the row vectors. Indexing is 0-based in each component."
(let ((vect (aref mat i)))
   (aset vect j elt)
   (aset mat i vect)
   mat))

But this doesn’t work:

(setq zero-row [0 0 0 0 0])
  => [0 0 0 0 0]

(setq zero-mat (make-vector 4 zero-row))
  => [[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]]

(matrix-set zero-mat 2 3 42)
  => [[0 0 0 42 0] [0 0 0 42 0] [0 0 0 42 0] [0 0 0 42 0]]

It looks like all the rows of the array are linked to the same vector, so changing that vector changes all the rows.

So two questions: (1) Why is this happening in the second case, but not the first? (2) How can I fix this (so I can access the (i, j)-th entry of a 2-dim. array represented this way)?

(I was originally writing a little routine to add two matrices, represented as vectors of vectors as above, and ran into the same problem. I think the stripped-down example above may make the problem clearer.)

  • 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-06-18T00:17:19+00:00Added an answer on June 18, 2026 at 12:17 am

    In the first case you are replacing an element in the “outer” vector by another vector (while other three “inner” vectors still point all to the same element). In the second case you replace an element in the “inner” vector (and you have only one inner vector duplicated four times, as per your example. A simple way to initialize vector to different distinct vectors would be something like this:

    (let ((i 0) (new-vector (make-vector 4 nil))
     (while (< (progn (aset new-vector i (make-vector 5 0))
                      (incf i))
               (length new-vector)))
    

    Sorry if there are any typos, was writing it in-place. But the idea should be simple enough to figure it out.

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

Sidebar

Related Questions

I want to build a 2 dimensional (non ragged at this point) object array.
I'm trying to figure out how to build a multi-dimensional array that is: flexible
I want to build a hierarchy from a one-dimensional array and can (almost) do
Would like to build a list of indices into a 2 dimensional bool_ array,
Is there a way in Ruby to make a copy of multi-dimensional array? I
I miss VB. Maybe that's sad... I need to make a 3 Dimensional array
I am trying to build a 2 dimensional array in C++ while I don't
I'm using numpy to build pixel arrays. An 800x600 image is an 3-dimensional array
I build EditText dynamically. Among other things, I set 2 properties: hint(.setHint) and inputType(.setInputType).
My build target is set for IOS5 which is where I understand UIButton.tintColor was

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.