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
SUMMARY

The discussion focuses on merging matrices in MATLAB by creating a 4x8 matrix filled with zeros and a 2x4 matrix containing the values [1 2 3 4]. The task is to replace the upper left-hand corner of the larger matrix with the values from the smaller matrix. The solution involves using MATLAB's indexing capabilities to directly assign the smaller matrix's values to the specified section of the larger matrix, allowing for efficient manipulation without the need for loops.

PREREQUISITES
  • Understanding of MATLAB matrix creation and manipulation
  • Familiarity with MATLAB indexing techniques
  • Basic knowledge of matrix dimensions and operations
  • Experience with MATLAB commands such as zeros() and meshgrid()
NEXT STEPS
  • Learn MATLAB matrix indexing techniques for efficient data manipulation
  • Explore the use of logical indexing in MATLAB
  • Study the differences between matrix operations and element-wise operations in MATLAB
  • Investigate advanced matrix manipulation functions in MATLAB, such as reshape() and cat()
USEFUL FOR

This discussion is beneficial for MATLAB users, particularly students and professionals in engineering and data analysis, who need to perform matrix operations and manipulations efficiently.

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
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
  • · Replies 3 ·
Replies
3
Views
3K