MATLAB: Merging Matrices - Create 0s Matrix & Replace with Values

In summary, you can create a 4x8 matrix with all elements equal to 0 and a smaller 2x4 matrix with values 1, 2, 3, and 4 in each row. Then, using indexing, you can replace the first 4 columns of the first 2 rows of the bigger matrix with the values from the smaller matrix, resulting in a modified big matrix. This can be done with just one instruction by using indexing to select the specific elements to be replaced. Alternatively, a for loop can also be used to achieve the same result.
  • #1
ineedhelpnow
651
0
Create a matrix with 4 rows and 8 columns with every element equal to 0. Create a second, smaller matrix with 2 rows and 4 columns where each row is [1 2 3 4]. Replace the 0s in the upper left-hand corner of the bigger matrix with the values from the smaller matrix. (If you do this correctly, the first 4 columns of the first 2 rows of the big matrix will change.)
Display the modified big matrix.

This is the last question i have. i promise (Blush)
what i did:
Code:
y=zeros(4,8)
z=meshgrid(1:4)

im not sure how to go about merging the second matrix into the first one now
 
Physics news on Phys.org
  • #2
Hi,

A hint:

A matrix in MATLAB it's just a sequence of coordinates paired with the object in that coordinate. So you can manipule this pairings in order to get whatever you want. In this case, change just a part of the matrix.

So you want to change the elements $y(i,j)$ for $1 \leq i \leq 2$ and $1\leq j \leq 4$ if I understood it correctly.

Can you see a way of doing this with just one instruction?
 
  • #3
With a for loop?
 
  • #4
Well,

That's a valid option, but there is a simpler way to do this with just one instruction.

If I give you a vector, say $v=ones(10,1)$, then you can select, for example the first five components with the command $v(1:5)$, or the 2-7 components by writing $v(2:7)$, and so on. So you can do something similar for solving the exercise.
 

1. How do I create a 0s matrix in MATLAB?

To create a 0s matrix in MATLAB, you can use the zeros function. This function takes in the dimensions of the desired matrix as input and returns a matrix filled with 0s.

2. Can I merge two matrices in MATLAB?

Yes, you can merge two matrices in MATLAB using the horzcat and vertcat functions. horzcat is used to concatenate matrices horizontally, while vertcat is used to concatenate them vertically.

3. How do I replace values in a matrix in MATLAB?

To replace values in a matrix in MATLAB, you can use logical indexing. This involves creating a logical matrix with the same dimensions as the original matrix and assigning the desired values to the corresponding logical indices in the matrix.

4. Is it possible to merge a matrix with a scalar value in MATLAB?

Yes, you can merge a matrix with a scalar value in MATLAB by using the repmat function. This function replicates the scalar value to match the dimensions of the matrix, allowing you to merge them together.

5. Can I merge matrices with different data types in MATLAB?

No, MATLAB does not allow you to merge matrices with different data types. If you try to merge matrices with different data types, you will receive an error. To merge matrices, they must have the same data type.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
891
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
877
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top