Duplicate the first row and the first column of a matrix - matlab

I have a 3*3 matrix A
A = [1 2 3
4 5 6
7 8 9];
I want to duplicate only the first row and column of this matrix. It should look like
1 1 2 3
1 1 2 3
4 4 5 6
7 7 8 9
can someone tell how can i do this in matlab

I think this is a good way just using indexing
A([1, 1:end], [1, 1:end])

You can do that by concatenating different parts of the original matrix:
B=[A(1) A(1,:);A(:,1) A];
In this expression A(1) is the top left element of A, A(1,:) is the first row and A(:,1) is the first column.
See the documentation on the colon operator.

In the code below, A is your starting point and I believe E is what you want to achieve.
You can of course combine all the intermediate expressions to achieve the final result in one step.
A= [1 2 3; 4 5 6; 7 8 9]
B= A(1:3,1:1)
C= [B A]
D= C(1:1,1:4)
E= [D;C]

A bit late in the game, but worthwhile answering. You can use padarray for that :
B = padarray(A,[1 1],'replicate','pre')
It's a one liner and more generic if you want to add more than just a single first and column ...

Related

Convert column matrix to multiple rows with duplicated data

In Matlab, given matrix 'a' as:
a = [1 2 3 4 5 6 7 8 9]';
but a few million rows deep, what would be the best way to convert it to the format shown in 'b'?
b = [1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9]
where each row in 'b' contains N consecutive values from 'a'?
I can write a function but was wondering if there's a faster, more built-in way?
Another option is the built-in Hankel matrix, for example in your case:
hankel(1:4,4:9)
or in the general case for a vector a and block size N
hankel(a(1:N),a(N:numel(a)))
I have no idea if the implementation of hankel is better or worse than #Luis Mendo solution, so test and see which solution is better for you...
Let
a = [1 2 3 4 5 6 7 8 9].'; % data
N = 4; % block size
If you have the Image Processing Toolbox: use im2col with the 'sliding' option:
b = im2col(a(:), [N 1], 'sliding');
Or, without any toolbox: use implict expansion (or bsxfun) to create the appropriate indexing matrix:
b = a((1:numel(a)-N+1)+(0:N-1).');

How to create a submatrix of a randomly filled matrix? [duplicate]

I have a 30 x 30 matrix, called A, and I want to assign B as the leftmost 30 x 20 block of A how can I do that?
Is this the correct way to do it?
B = A[30 ; 20]
No the correct way is
B = A(:, 1:20);
where : is shorthand for all of the rows in A.
Matrix indexing in MATLAB uses round brackets, (). Square brackets, [], are used to declare matrices (or vectors) as in
>> v = [1 2 3; 4 5 6; 7 8 9]
v =
1 2 3
4 5 6
7 8 9
excaza provides a very good link on Matrix Indexing in MATLAB which should help you. There is also Matrix Indexing.
A_new = A(:,1:20)
takes all the rows from A with this part A(:,) and the first 20 columns with this part A(,1:20)
A_newis now 30x20
You can also iterate over elements in two loops, but the above answer is easiest

how to assign part of a matrix to other matrix in matlab

I have a 30 x 30 matrix, called A, and I want to assign B as the leftmost 30 x 20 block of A how can I do that?
Is this the correct way to do it?
B = A[30 ; 20]
No the correct way is
B = A(:, 1:20);
where : is shorthand for all of the rows in A.
Matrix indexing in MATLAB uses round brackets, (). Square brackets, [], are used to declare matrices (or vectors) as in
>> v = [1 2 3; 4 5 6; 7 8 9]
v =
1 2 3
4 5 6
7 8 9
excaza provides a very good link on Matrix Indexing in MATLAB which should help you. There is also Matrix Indexing.
A_new = A(:,1:20)
takes all the rows from A with this part A(:,) and the first 20 columns with this part A(,1:20)
A_newis now 30x20
You can also iterate over elements in two loops, but the above answer is easiest

Incremental matrix without loop

(Just approaching Matlab for personal understanding), suppose I have a Z,Y matrix in this case Z=1 Y=3
A=1:3
output: 1 2 3
Now I need to increase the matrix vertically to obtain:
1 2 3
2 4 6
3 6 9
How can I achieve that without using a loop?
The easiest way is to use vector multiplication.
If your goal is to obtain
1 2 3
2 4 6
3 6 9
given A=1:3
all you have to do is
A.'*A
This will take the vector product of the transpose (.') of A with A itself
Another way is to use bsxfun:
A = [1 2 3];
B = bsxfun(#times, A.', A);
This is essentially the same answer as Federico's where the outer product of the vector is taken.

Selecting elements in matrix using matlab

My problem is this: I've a matrix, as example
1 2 3
4 2 6
6 1 8
4 5 4
7 1 5
8 2 0
I wish to extract selected values from the matrix, as example, a vector like this
B = [3 6 0]
selecting third column values when the value in the second column is 2.
I tried in different ways, but no one of these works.
Use this -
B = A(A(:,2)==2,3)' %// Assuming A is your input matrix
If M is your Matrix, you can select the second column using
M(:,2)
Compare it to two to get the lines which contain a 2
M(:,2)==2
And use this logical vector to select your elements from the third column.
M(M(:,2)==2,3)
A little more generally: if you want to select based on a set of values, use ismember to generate the logical index:
>> A(ismember(A(:,2), [2 5]) , 3) %// [2 5]: values you want to find in 2nd col
ans =
3
4
6
0