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

  • Context: MATLAB 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Matlab Matrices
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 3K views
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.