How to convert 1D to 2D by Matlab program - matlab

I would like to ask a question about Matlab program.
I have vector a
a = [1 2 3 4 5 6 7 8 9 10 11 12];
I would like to convert vector a to 2D array. Normally, I use this code to convert it.
m =1;
for i=1:4
for j=1:3
b(i,j) = a(m);
m=m+1;
end
end
Then b is a 2D matrix.
b =
1 2 3
4 5 6
7 8 9
10 11 12
Anybody, have an idea to convert 1D to 2D without using loop.
Thanks,

Check out the reshape function and help page.
In particular,
B = reshape(A,m,n)
returns the m-by-n matrix B whose elements are taken column-wise from A. An error results if A does not have m*n elements.
Note that it is column-wise, so I suggest you make a matrix with 3 rows and 4 columns and then tip it on its side (A.' will take the transpose of a matrix).

Related

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 create more than one matrix in a row using matlab

I am trying to get a series of vectors which come from the same original, to make an easy example, suppose this vector V= (1,2,3,4,5,6,7,8,9,10) (of course mine is bigger)
The first vector has to look like this:
R1=(1,3,5,7,9)= V(1:1:end)
The second vector:
R2=(2,4,6,8,10)=V(2:1:end)
The third vector:
R3=(3,5,7,9)=V(3:1:end)
The fourth vector:
R4=(4,6,8,10)=V(4:1:end)
...
R8=(8,10)=V(8:1:end)
So my questions are:
Is there an easier way to get this result?
How can I know the total number of Ri vectors with distance = 1 that can obtained from V?
Use Matlab's cell object which can hold a vector in every cell.
Use a for loop to fill this cell object gradually.
Code example:
%initialize V
V= [1,2,3,4,5,6,7,8,9,10];
%initialize an empty cell of size [10,1]
R= cell(length(V)-2,1);
%fill the cell
for ii=1:length(R)
R{ii} = V(ii:2:end);
end
%prints results
for ii=1:length(R)
R{ii}
end
Results (each row is a different vector):
1 3 5 7 9
2 4 6 8 10
3 5 7 9
4 6 8 10
5 7 9
6 8 10
7 9
8 10

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

How to convert 1D vector to 2D vector and user defined rows and columns in matlab?

I have a 1x10 1D vector A=1:10; and I want to convert it into a 5x2 2D vector
is there any function I can use like:
2dA = 1Dto2D(A,5,2);
and I get
2dA = [ 1 2 3 4 5
6 7 8 9 10]
Yes, you are looking for the reshape function. It works exactly as you have described.

Reconstruct matrix from diagonals in matlab

Given a vector of the counter-diagonals of a matrix in matlab, is there an easy way to reconstruct the matrix?
For example, given
x = [1 2 3 4 5 6 7 8 9]
is there any easy way to reconstruct it to the following?
1 2 4
3 5 7
6 8 9
This is made slightly easier by the fact that the dimensions of the original block are known. Reconstructing a rotation or transposition of the original matrix is fine, since rotating and transposing are easy to undo. Faster is better, this calculation has to be done on many xs.
Thanks!
You can create the corresponding Hankel matrix and use it for sorting (works only if the output is a square matrix!):
x = [1 2 3 4 5 6 7 8 9];
%# find size of output (works only with square arrays)
n=sqrt(length(x));
%# create Hankel matrix
hh = hankel(1:n,n:(2*n-1));
%# sort to get order of elements (conveniently, sort doesn't disturb ties)
[~,sortIdx]=sort(hh(:));
%# reshape and transpose
out = reshape(x(sortIdx),n,n)'; %'# SO formatting
out =
1 2 4
3 5 7
6 8 9