MATLAB, single command for multiple columns

Click For Summary

Discussion Overview

The discussion revolves around creating a specific matrix in MATLAB using a single command, focusing on the challenge of populating multiple columns with ordered numbers while maintaining a specific structure. The context is primarily homework-related, with participants sharing their attempts and solutions.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant describes their attempt to create a matrix using multiple commands and encounters a dimension mismatch error when trying to assign values in one command.
  • Another participant suggests using transposition to achieve the desired matrix structure and provides an alternative brute force method involving zeros and matrix concatenation.
  • A later reply acknowledges a mistake in transposing and expresses gratitude for the guidance received.
  • One participant emphasizes the importance of sharing the exact code used when troubleshooting issues to facilitate better assistance.
  • Another participant notes that the definition of a "one line" command can include semicolons, which may affect how the solution is structured.

Areas of Agreement / Disagreement

Participants generally agree on the methods to approach the problem, but there is no consensus on the best way to define a "one line" command in MATLAB, as well as the specifics of transposing matrices.

Contextual Notes

Participants express varying levels of familiarity with MATLAB, and some limitations arise from misunderstanding the syntax and structure of matrix assignments. There are also unresolved aspects regarding the precise definition of a single command in the context of MATLAB.

Who May Find This Useful

Individuals learning MATLAB, particularly those focused on matrix manipulation and assignment, may find this discussion beneficial.

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 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K