Message Boards Message Boards

0
|
3020 Views
|
5 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Padding a vector using Insert[ ]?

Hello, I am looking for some help with the INSERT command in Mathematica. I have a 4 x1 column vector

enter image description here

In which i have inserted 4 additional rows of zeros at the top, thus making it a (8 x 1) column vector.

enter image description here enter image description here

If you notice that the original terms that now occupy the 5h, 6th, 7th & 8th values, are now enclosed in braces { }.

So it seems that the new 8 x 1 column vector is cannot be multiplied with a 8 x 8 matrix, which I was trying to do and resulted in this error.

I get this message enter image description here

Would you know what the issue is with the INSERT command in my case?

Thanks

POSTED BY: Aslam Kittur
5 Replies

Hi Daniel. Thanks for clarifying it further.

My code is fixed and works now!

Very much appreciated.

-Aslam

POSTED BY: Aslam Kittur

It is best to provide minimal examples of the problem at hand. The example notebook is far from minimal.

The Print statements indicate there is a problem here. DisplVector = Insert[NDisp, 0, {{1}, {1}, {1}, {1}}];

It places zeros, rather than the list {0}, inside a matrix. Which makes it no longer a matrix. Neil Singer already explained this issue so I won't repeat that.

POSTED BY: Daniel Lichtblau

Hi Neil. Thanks very much for your reply. I did try the 'Flatten' first and then tried the 'Join' command, neither of them worked in my case. I can seem to understand, why the LinearSolve command creates a matrix when its supposed to be a vector.

I have attached the code, and would greatly appreciate your help.

Regards,

Attachments:
POSTED BY: Aslam Kittur

Aslam,

You should normally post your code using the third button on the formatting bar. Without seeing an example that reproduces your problem, it is harder to answer.

Also, You should never name anything beginning with capital letters because you might interfere with the internal workings of Mathematica.

it appears to me that you are not creating a vector in NDisp but a matrix:

vector = {1,2,3,4}
matrix = Transpose[{{1,2,3,4}}]

which is the same as

matrix = {{1},{2},{3},{4}}

To add elements, normally I use Join (although Insert works)

dv = Join[{0, 0, 0}, nd]

or

dv = Join[ConstantArray[0, 3], nd]

To fix your issue, you can convert your matrix to a vector:

Flatten[matrix]

or you can convert your inserted vector to a matrix

Join[Transpose[{{0, 0, 0}}, nd] 

or

Join[Transpose[{ConstantArray[0, 3]}], nd]

You can see if the structure of what you create is consistent by doing a

dv //FullForm 

to see the internal representation of your expression.

Regards

Neil

POSTED BY: Neil Singer

Thanks again. My code is fixed and works now!

Very much appreciated.

-Aslam

POSTED BY: Aslam Kittur
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract