how to fill matrix?

15 views (last 30 days)
Mira le
Mira le on 15 Aug 2021
Answered: Awais Saeed on 15 Aug 2021
Hello every one
I want to fill matrix with two vector like this:
first vector: 1 2 3 4 5
second vector 3 5 1 4
matrix is as follow:
1 2 3 4 5
3 0 1 1 0 0
5 1 1 0 0 1
1 0 1 0 1 0
4 0 1 1 0 1
How can I put both vectors in my matrix like this?, where the first cellule is empty
Thank you very much.

Accepted Answer

Awais Saeed
Awais Saeed on 15 Aug 2021
I do not think that you can create such a matrix because there is a column dimension mis-match. You can add zero padding to get such a result
A = [1 2 3 4 5];
B = [3 5 1 4];
M = [0 1 1 0 0;1 1 0 0 1;0 1 0 1 0;0 1 1 0 1];
M = horzcat(B',M); % horizonal concatenation
% size(A,2) is not equal to size(M,2). To make them equal add zeros in A
M = vertcat([zeros(1,length(M)-length(A)) A],M) % vertical concatenation

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!