1

How do I add 1 position and 1 position only in a 2d array. I tried adding the array value like a 1d array but multiple element were added. Can anyone help me?

This is a 2d array:

1

Is it possible to do this:

2

  • Unclear question, please revise. – Casper Beyer Aug 10 '14 at 01:31
  • It seems that OP tries to resolve the same problem than in [how-to-turn-list-into-a-square-list](http://stackoverflow.com/questions/25224602/how-to-turn-my-doubly-linked-list-container-into-a-square-list-container) but with array. – Jarod42 Aug 10 '14 at 01:47

1 Answers1

3

It's not possible. Every language I know of requires matrices to be "rectangular." I would recommend either using a 2X2 matrix plus a variable, or a column or row vector of length five. You can also create a 3X2 or 2X3 matrix and just choose to leave one element as NaN or 0 etc. I may be able to answer your question better if you leave a comment telling me the reasoning behind wanting a non rectangular matrix.

EDIT: I was wrong you can create non rectangular matrices in Java link.

Community
  • 1
  • 1
user3854447
  • 324
  • 3
  • 15
  • Java is one exception to your "rectangular matrices" statement. I believe that there are other languages that allow this kind of feature, too. – Natan Streppel Aug 10 '14 at 01:47
  • It depend the data structure used for storage the matrix, for example `std::vector>` would let you do the behavior asked if you want, i understand that would not be a matrix anymore, but let you do it. – NetVipeC Aug 10 '14 at 01:51
  • These folks are right! You can create non rectangular arrays in Java (also C#). Here is a link showing you how [link](http://stackoverflow.com/questions/10271540/java-jagged-array). I will edit my post to reflect this change. Thank you @Streppel! – user3854447 Aug 10 '14 at 01:55
  • Now that I've read @NetVipeC's comment, I realized that my comment kind of twisted the concept of a matrix. If I could edit it back again, I'd written array of arrays, which is what they are actually called, and not a matrix. – Natan Streppel Aug 10 '14 at 01:56