MATLAB, single command for multiple columns

In summary, the conversation was about creating a specific matrix using one command. The individual elements had to be typed in a specific order and the conversation included attempts to solve the problem in one command, with the help of transposing and using the apostrophe.
  • #1
moouers
80
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
  • #2
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:
  • #3
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
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 ;)
 
  • #5




There are a few ways to create this matrix in one command using MATLAB. One way is to use the colon operator to create a vector of the desired elements and then reshape it into a matrix with the correct dimensions. The command would look like this:

e(2:4,3:5) = reshape([1:9], [3 3])'

Another way is to use the repmat function to repeat a vector multiple times and then use the colon operator to create the desired matrix. The command would look like this:

e(2:4,3:5) = repmat([1 4 7], 3, 1) + repmat([0:2]', 1, 3)

Both of these commands will create the desired matrix in one command.
 

1. What is the syntax for using a single command to operate on multiple columns in MATLAB?

The syntax for using a single command to operate on multiple columns in MATLAB is:command_name(matrix_name(:,column_indices))

2. Can I use a single command to perform different operations on multiple columns in MATLAB?

Yes, you can use a single command with different arguments to perform different operations on multiple columns in MATLAB. For example, you can use the "sum" command to find the sum of values in multiple columns, and the "mean" command to find the average of values in the same columns.

3. How do I select specific columns to operate on using a single command in MATLAB?

You can select specific columns to operate on by specifying their column indices in the command. For example, if you want to operate on columns 2, 4, and 6, you would use the command matrix_name(:,[2 4 6]).

4. Is it possible to use a single command to operate on columns in a specific order in MATLAB?

Yes, you can use the "order" argument in the command to specify the order of columns in which you want the operation to be performed. For example, if you want to operate on columns 2, 4, and 6 in the order 4, 6, 2, you would use the command matrix_name(:,[2 4 6],'order',[4 6 2]).

5. Can I use a single command to perform operations on non-contiguous columns in MATLAB?

Yes, you can use the "step" argument in the command to specify the step size between the columns you want to operate on. For example, if you want to operate on columns 1, 4, 7, and 10, you would use the command matrix_name(:,[1 4 7 10],'step',3).

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Nuclear Engineering
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
881
  • Engineering and Comp Sci Homework Help
Replies
2
Views
825
  • Engineering and Comp Sci Homework Help
Replies
4
Views
926
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Back
Top