Homework Help: MATLAB, single command for multiple columns

1. Jul 15, 2013

moouers

1. The problem statement, all variables and given/known data

Create the following matrix by typing one command. Do not type individual elements explicitly.

\begin{array}{ccc}
0 & 0 & 0 & 0 & 0 \\
0 & 0 & 1 & 4 & 7 \\
0 & 0 & 2 & 5 & 8 \\
0 & 0 & 3 & 6 & 9 \\ \end{array}

2. Relevant equations
None

3. The attempt at a solution

A previous problem was very similar, except the "1 2 3" etc elements were in a row rather than a column. For that problem, I came up with e(2:4,3:5)=[1:3;4:6;7:9] and it worked. For this problem, however, I can seem to only get it to work if I do it in more than one command, such as:

>> e(2:4,3)=[1:3],e(2:4,4)=[4:6],e(2:4,5)=[7:9]

I have no idea how to do this in one command. I tried the following but got the "Subscripted assignment dimension mismatch." error:

e(2:4,3:5)=[1:3,4:6,7:9]

I also tried transposing, but got nowhere fast. This isn't for a grade (self-teaching so the requirement can be waived as per agreement), but I need to become proficient at this program. Any help is greatly appreciated.

2. Jul 16, 2013

Simon Bridge

If you have the 3x3 matrix already, how would you insert it in the bottom-right corner of a 4x5 matrix?

Oh I see ... you don't know how to do the numbers ordered in columns.
What was wrong with transposing?

e(2:4,3:5)=[1:3;4:6;7:9]'

or, by brute force:

[zeros(4,2) [zeros(1,3); [1:3;4:6;7:9]' ]]

Last edited: Jul 16, 2013
3. Jul 16, 2013

moouers

Thank you very much, Simon. When I tried transposing, I put the apostrophe in the wrong place (inside the bracket) and didn't catch it. Still very new to this.

4. Jul 17, 2013

Simon Bridge

No worries - normally I wouldn't just write out the answer for you but you were soooo close I figured it was something like that.
Hint: In future you should provide the code you actually used when you say something didn't work.
Copy and paste it from the commandline if possible... as well as your reasoning.

Don't worry about looking silly, we've all been there ;) ... anyway, science is about being wrong: your mistake could help someone else.

Note: when someone asks for "one line", check the definition of a line ... if it is everything from the start to the "newline" or "CR" character, then you get to include ";"'s in your "one line" and, therefore, more than one command ;)