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

  • Context: MATLAB 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Matlab Matrices
Click For Summary

Discussion Overview

The discussion revolves around merging two matrices in MATLAB, specifically creating a zero matrix and replacing a portion of it with values from a smaller matrix. The focus is on the technical implementation of this task using MATLAB commands.

Discussion Character

  • Homework-related, Technical explanation

Main Points Raised

  • One participant describes the task of creating a 4x8 zero matrix and a 2x4 matrix filled with specific values, seeking guidance on how to merge them.
  • Another participant suggests that a matrix in MATLAB can be manipulated by changing specific elements using their coordinates, hinting at a solution that involves a single instruction.
  • A different participant proposes the use of a for loop as a method to achieve the merging, indicating it as a valid approach.
  • One participant counters that there is a simpler method than using a for loop, referencing how to select components of a vector to illustrate a more efficient solution.

Areas of Agreement / Disagreement

Participants express differing views on the best method to merge the matrices, with some advocating for a for loop and others suggesting a more efficient single-instruction approach. No consensus is reached on the preferred method.

Contextual Notes

The discussion does not clarify the specific MATLAB commands needed to implement the proposed solutions, and there may be assumptions about the participants' familiarity with MATLAB syntax and matrix manipulation.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K