MATLAB, single command for multiple columns

AI Thread Summary
The discussion revolves around creating a specific 4x5 matrix in MATLAB using a single command without explicitly typing individual elements. A user initially struggles with the correct syntax and faces a "dimension mismatch" error when attempting to assign values to multiple columns simultaneously. Solutions provided include using transposition correctly and employing a combination of zeros and matrix concatenation to achieve the desired matrix format. The importance of sharing the exact code used when encountering errors is emphasized for effective troubleshooting. Overall, the conversation highlights common challenges faced by beginners in MATLAB and offers practical coding tips.
moouers
Messages
80
Reaction score
0

Homework Statement



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}

Homework Equations


None


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.
 
Physics news on Phys.org
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:
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.
 
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 ;)
 

Similar threads

Replies
10
Views
3K
Replies
1
Views
2K
Replies
4
Views
1K
Replies
1
Views
2K
Replies
6
Views
2K
Replies
21
Views
3K
Back
Top