How to insert an element into matrix?

Suppose I have a matrix A = ones(4,6). I want to combine it with a vector B = zeros(1,6) to make another matrix C of size 5 by 6, where the first four rows would be ones and fifth would be zero.

Antworten (2)

Image Analyst
Image Analyst am 22 Aug. 2017

2 Stimmen

Here's code to do both inserting (like your subject line) and appending (like your message body):
A = ones(4,6)
B = zeros(1,6)
% To append / concatenate:
C = [A; B]
% To insert into a specified row number:
rowToInsert = 3 % Whatever you want.
C = [A(1:rowToInsert-1, :); B; A(rowToInsert:end, :)]

4 Kommentare

Nitesh khanna
Nitesh khanna am 17 Jul. 2020
how to get variables with suffix , in matrix for..as following
x1 y1 txy1
x2 y2 txy2
x3 y3 txy3
x4 y4 txy4
I don't know what you mean. What suffix? Do you have scalar variables with those names and want to build a matrix from them? Like
M = [...
x1 y1 txy1;
x2 y2 txy2;
x3 y3 txy3;
x4 y4 txy4]
Amay Gupta
Amay Gupta am 14 Okt. 2020
can the first one be used in loops?
if yes please explain.
Image Analyst
Image Analyst am 14 Okt. 2020
What is "the first one"? Please explain. Why do you want loops?

Melden Sie sich an, um zu kommentieren.

José-Luis
José-Luis am 22 Aug. 2017
Bearbeitet: José-Luis am 22 Aug. 2017

0 Stimmen

result = [A;B]
Or use cat()

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Produkte

Tags

Gefragt:

am 22 Aug. 2017

Kommentiert:

am 14 Okt. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by