Concatenating 2D Array: Adding Rows | Step-by-Step Guide for Problem 1(d)"

  • Thread starter gfd43tg
  • Start date
  • Tags
    2d Array
In summary, the conversation discusses how to add a row into an existing array in MATLAB. The first step is to create a new row using the zeros command or by extracting relevant rows from the existing array. Then, the new row can be added using the semicolon operator.
  • #1
gfd43tg
Gold Member
950
50

Homework Statement


I'm working on problem 1, specifically part (d) in the attached PDF file. I will show my code for (a) - (c) so you can see where I am coming from

Homework Equations


The Attempt at a Solution



(a)
Code:
  A1 = [1.3 5.2; 7.4 3.8; 9 2]

A1 =

    1.3000    5.2000
    7.4000    3.8000
    9.0000    2.0000
(b)
Code:
A1(2)

ans =

    7.4000

(c)
Code:
B = [1 1 1];
A3 = [A1 B']

A3 =

    1.3000    5.2000    1.0000
    7.4000    3.8000    1.0000
    9.0000    2.0000    1.0000

(d)
I don't know how to add a row into the array for any given row. I know if I wanted to add for example [0 0] to the end of the row

Code:
C = [0 0]

C =

     0     0

A4 = [A1; C]

A4 =

    1.3000    5.2000
    7.4000    3.8000
    9.0000    2.0000
         0         0

but I just need to move that [0 0] into the second row
 

Attachments

  • ARRassignment.pdf
    118.9 KB · Views: 351
Last edited:
Physics news on Phys.org
  • #2
I think you have to use [xxx; C ; yyy] where xxx and yyy extract the relevant rows from A1.

Also the problem told you to use the zeros command, which you could put in place of making the intermediate matrix C.
 
  • Like
Likes 1 person

1. How do you concatenate two 2D arrays?

To concatenate two 2D arrays, you can use the np.concatenate() function from the NumPy library. This function takes in two arrays as arguments and combines them along the specified axis.

2. What is the purpose of adding rows in a 2D array?

Adding rows in a 2D array can be useful for expanding the size of the array or for combining data from multiple arrays into one larger array. It can also help with organizing and structuring data in a more efficient way.

3. How does concatenating rows differ from concatenating columns in a 2D array?

Concatenating rows and columns in a 2D array differ in terms of which axis they are combined along. Concatenating rows combines data along the vertical axis, while concatenating columns combines data along the horizontal axis.

4. Can you concatenate arrays with different dimensions?

No, arrays with different dimensions cannot be concatenated. The arrays must have the same shape along the axis being concatenated in order for the operation to be successful.

5. Are there any limitations to concatenating 2D arrays?

One limitation to concatenating 2D arrays is that the number of dimensions cannot be changed. For example, if you concatenate two 2D arrays, the resulting array will also be a 2D array. Additionally, the arrays being concatenated must have the same data type.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
888
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Calculus and Beyond Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
939
Back
Top