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

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Matlab Matrices
AI Thread Summary
To create a 4x8 matrix filled with zeros in MATLAB, use the command y=zeros(4,8). For the smaller 2x4 matrix, define it as z=[1 2 3 4; 1 2 3 4]. To replace the upper left corner of the larger matrix with the values from the smaller matrix, use the command y(1:2, 1:4) = z. This single instruction effectively updates the specified portion of the larger matrix. Display the modified matrix to see the changes reflected.
ineedhelpnow
Messages
649
Reaction score
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
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?
 
With a for loop?
 
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.
 
Back
Top