Azzera filtri
Azzera filtri

How to add rows containing zeros to a matrix

33 visualizzazioni (ultimi 30 giorni)
I have a "2x101" matrix, I want to add 4 rows to it, containing "zeros" to make it "6x101" matrix.
Please if anyone could guide me on syntax. Thankyou.

Risposta accettata

Cris LaPierre
Cris LaPierre il 27 Lug 2023
A = rand(2,6)
A = 2×6
0.8131 0.2963 0.1224 0.8259 0.2399 0.5947 0.7303 0.1732 0.9839 0.2746 0.6453 0.7036
A(3:4,:)=0
A = 4×6
0.8131 0.2963 0.1224 0.8259 0.2399 0.5947 0.7303 0.1732 0.9839 0.2746 0.6453 0.7036 0 0 0 0 0 0 0 0 0 0 0 0

Più risposte (3)

Voss
Voss il 28 Lug 2023
Here's one way:
% a 2x101 matrix
a = rand(2,101)
a = 2×101
0.2749 0.6630 0.7375 0.2538 0.1755 0.5986 0.1725 0.4662 0.3609 0.7840 0.8763 0.3446 0.3603 0.8626 0.2283 0.1468 0.3254 0.3290 0.3960 0.4185 0.1099 0.0486 0.0626 0.7403 0.6850 0.4095 0.8703 0.1152 0.5754 0.7633 0.8014 0.8300 0.6248 0.5979 0.5868 0.4567 0.0246 0.0519 0.1590 0.1996 0.5126 0.0511 0.1397 0.9698 0.5512 0.3256 0.6557 0.6494 0.0743 0.7528 0.8313 0.6114 0.4597 0.6038 0.5030 0.9302 0.4747 0.8711 0.4699 0.2633
% append 4 rows of zeros
a = [a; zeros(4,size(a,2))]
a = 6×101
0.2749 0.6630 0.7375 0.2538 0.1755 0.5986 0.1725 0.4662 0.3609 0.7840 0.8763 0.3446 0.3603 0.8626 0.2283 0.1468 0.3254 0.3290 0.3960 0.4185 0.1099 0.0486 0.0626 0.7403 0.6850 0.4095 0.8703 0.1152 0.5754 0.7633 0.8014 0.8300 0.6248 0.5979 0.5868 0.4567 0.0246 0.0519 0.1590 0.1996 0.5126 0.0511 0.1397 0.9698 0.5512 0.3256 0.6557 0.6494 0.0743 0.7528 0.8313 0.6114 0.4597 0.6038 0.5030 0.9302 0.4747 0.8711 0.4699 0.2633 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Les Beckham
Les Beckham il 27 Lug 2023
Here is another way.
A = rand(2,6)
A = 2×6
0.5677 0.5655 0.2207 0.5221 0.5033 0.0094 0.1184 0.9091 0.7727 0.4403 0.6020 0.3454
A(4,6) = 0
A = 4×6
0.5677 0.5655 0.2207 0.5221 0.5033 0.0094 0.1184 0.9091 0.7727 0.4403 0.6020 0.3454 0 0 0 0 0 0 0 0 0 0 0 0

James Tursa
James Tursa il 27 Lug 2023
Modificato: James Tursa il 27 Lug 2023
E.g., one way using the end syntax:
my_matrix = reshape(1:12,3,4)
my_matrix = 3×4
1 4 7 10 2 5 8 11 3 6 9 12
n = 4; % number of rows to append
my_matrix(end+n,1) = 0
my_matrix = 7×4
1 4 7 10 2 5 8 11 3 6 9 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Basically, setting any element in the 4th row beyond the current last row will cause MATLAB to append 4 rows of 0's to the matrix and set the particular element. I.e., MATLAB will dynamically increase the size of the matrix to accomodate the element you are trying to set, and all of the elements you don't set in the process are automatically set to 0's. In the above example, I set the (7,1) element to 0 explicitly, and MATLAB filled in all the other elements in the newly created four rows to 0's automatically.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Prodotti


Release

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by