Change value in matrix [duplicate] - matlab

This question already has answers here:
Replace specific columns in a matrix with a constant column vector
(4 answers)
Closed 6 years ago.
I try to change the values from matrix A below.
A=[1; 1; 2; 2]
A =
1
1
2
2
which want to change into target matrix B
B = [1 0;1 0;0 1;0 1]
B =
1 0
1 0
0 1
0 1
I can not change, please guide me some example.

using bsxfun You can write:
B = bsxfun(#eq,A,1:2)
or in Octave or Matlab R2016b:
B = A==1:2

Related

How to fill the matrix with 0 in matlab [duplicate]

This question already has answers here:
How can I put margins in an image?
(4 answers)
Closed 7 years ago.
Suppose I have a matrix [1 2;3 4], but I need [0 0 0 0;0 1 2 0;0 3 4 0;0 0 0 0], I now how to do it, but is there a function can solve it within one line?
If you have the image processing toolbox, use padarray:
>> A = [1 2; 3 4];
>> B = padarray(A, [1 1])
B =
0 0 0 0
0 1 2 0
0 3 4 0
0 0 0 0
The first input is the matrix you want to pad, and the second input is how many zeroes along the border in each dimension you want to see. You want a 1 element zero border both horizontally and vertically, and so [1 1] is what is required.
However, I'm confused as to why you want this to be in "one-line". If you want a one element border surrounding the original matrix, what's wrong with having multiple lines?
A = [1 2; 3 4];
B = zeros(size(A) + 2);
B(2:end-1,2:end-1) = A;
This is three lines of code, including the definition of your original matrix, but each line is quite clear. You define a new matrix that has 2 more rows and 2 more columns that the original, because you want a 1 element zero boundary around the original matrix, then just place it in the middle.

How to generate label matrix from a label vector? [duplicate]

This question already has answers here:
Replace specific columns in a matrix with a constant column vector
(4 answers)
Closed 8 years ago.
I've a label vector as follows:
y=[3 1 5 3 4 2];
Is there any efficient way to generate the following label matrix?
[0 0 1 0 0;
1 0 0 0 0;
0 0 0 0 1;
0 0 1 0 0;
0 0 0 1 0;
0 1 0 0 0;]
UPDATED:
This post and this post are both good answers. Using the scripts provided by #Nras, the following is to handle the missing labels:
Y=[3 1 5 3 4 2];
labels=unique(Y);
[~,indexes]=ismember(Y,labels);
rows = 1:length(Y); %// row indx
T = zeros(length(Y),length(unique(indexes))); %// A matrix full of zeros
T(sub2ind(size(T),rows ,indexes)) = 1; %// Ones at the desired row/column combinations
Use sub2ind for this problem. Your y determines the columns to use,
the rows are simply always incremented by 1. By using sub2ind the desired row-column-combinations get transformed to a linear index and can then all be adressed in a vectorized way.
y = [3 1 5 3 4 2]; %// column indx
rows = 1:length(y); %// row indx
M = zeros(length(y), max(y)); %// A matrix full of zeros
M(sub2ind(size(M),rows ,y)) = 1; %// Ones at the desired row/column combinations

generate plot matrix from two different matrices in matlab [duplicate]

This question already has an answer here:
Create a larger matrix in special case [closed]
(1 answer)
Closed 9 years ago.
I have two matrices (5_by_1), say A=[5 rows,1 column] and B=[5 rows, 1 column] if I do plot(A,B), I will create a large matrix C=[5 rows,5 columns] wright?!
Now I would like to create this large matrix without plot it. I want this matrix directly. thank you.
For example A=[1 2 3 4 5 ] and B=[3 4 2 1 4]
c=
0 1 0 0 0
1 0 0 0 1
0 0 1 0 0
0 0 0 1 0
This should work:
a = [1 2 3 4 5];
b = [3 4 2 1 4];
c = flipud(sparse(b,a,1,4,5));
If you want to see the full c:
full(c)
or if you have a bigger version:
c = flipud(sparse(b,a,1,max(b),max(a)));
The flipud command is to flip the matrix upside down.
Hope this helps =)
EDIT:
"Shift" the matrices, so that your lowest value is in (1,1) (before you flip it). The structure will be correct, but the origin won't be easy to spot.
a_1 = floor(a - min(a)) + 1; % floor if you don't have integers.
b_1 = floor(b - min(b)) + 1;
c = flipud(sparse(b_1,a_1,1,max(b_1),max(a_1)));

Matrix to Diagonal Matrix in MATLAB [duplicate]

This question already has answers here:
How to vectorize row-wise diagonalization of a matrix
(4 answers)
Closed 9 years ago.
Let's say I have a matrix in MATLAB like
A = [1 2 3;
4 5 6;
7 8 9]
and I would like to obtain a matrix of the form
B = [1 0 0;
0 4 0;
0 0 7;
2 0 0;
0 5 0;
0 0 8;
3 0 0;
0 6 0;
0 0 9]
i.e. a matrix that is a concatenation of three diagonal matrices, with each having the columns of matrix A at their diagonals. I know how to do this using a for loop over the columns of A and then concatenating all the results but I am looking for a shorter way to do this. Please share your ideas.
B(repmat(eye(3),3,1)==1) = A;
reshape(B, [], 3)
Here's a way using linear indexing:
B(sub2ind([9 3], 1:9, mod(0:8,3)+1))=A;
reshape(B,9,3)
If you want this to be generic, realize that each column of the original becomes a diagonal. Therefore, the number of rows in the original becomes the number of columns in the output, and 3 rows x cols becomes the number of rows. The rest of the answer doesn't change at all:
c = size(A,1);
r = size(A,1) * size(A,2); #% or prod(size(A));
B(sub2ind([r c], 1:r, mod(0:(r-1),c)+1)) = A;
B = sparse( 1:numel(A), repmat( 1:size(A,2), [1 size(A,1)] ),...
A(:), numel(A), size(A,2));
should do the trick.
You can B = full(B); if you want a full matrix

How to find n largest elements in an array and make the other elements zero in matlab? [duplicate]

This question already has answers here:
Get the indices of the n largest elements in a matrix
(4 answers)
Closed 8 years ago.
Suppose I have a matrix
A=[2 3 4; 6 1 2]
I want to find 2 largest elements and make all the other elements zero.
In this case A finally becomes
A=[0 0 4; 6 0 0]
Your line of action should be:
Sort the matrix in descending order and obtain the order of the indices of the sorted elements.
Discard the first two indices and use the rest to zero out the corresponding elements in A.
Example
A = [2 3 4; 6 1 2];
[Y, idx] = sort(A(:), 'descend')
A(idx(3:end)) = 0
This should result in:
A =
0 0 4
6 0 0
>> A=[2 3 4; 6 1 2]
A =
2 3 4
6 1 2
>> [~,idx] = sort(A(:), 'descend');
>> A(idx(3:end))=0
A =
0 0 4
6 0 0