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

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.

Related

Change value in matrix [duplicate]

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

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

How to add boundary to a matrix

I am quite new to MATLAB. I would like to know how can I transfer the matrix A to matrix B as shown below?
A = 1 2
3 4
5 6
B=0 0 0 0
1 1 2 1
1 3 4 1
1 5 6 1
0 0 0 0
Essentially I would like to add a boundary to A.
Thank you!
padarray implementation -
%// pad ones on left-right and then pad zeros on top-bottom
B = padarray(padarray(A,[0 1],1),[1 0],0)
If I understand your question correctly, you wish to insert a 1 element boundary surrounding the matrix. In that case, try something like this:
A = [1 2; 3 4; 5 6];
[rows,cols] = size(A);
B = zeros(rows+2, cols+2);
B(2:end-1,[1 end]) = 1;
B(2:end-1,2:end-1) = A;
However, you can also use padarray like what #Divakar has suggested. Much more elegant!

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)));

Boundaries and contours in Matlab

I have a binary image with non closed curves of 1-pixel width on it. I want to grab this curves as a list of points (in proper order). I found bwboundaries function which tries to wrap all non-zero pixels and thus in this case returns duplicate points:
>> A = [0 0 0; 1 1 1; 0 0 0];
>> b = bwboundaries(A)
ans =
[5x2 double]
>> b{1}
ans =
2 1
2 2
2 3
2 2
2 1
bwtraceboundary do the same
>> bwtraceboundary(A, [2 1], 'E')
ans =
2 1
2 2
2 3
2 2
2 1
Is there any standard method to get matrix like [2 1; 2 2; 2 3] immediately?
It produces double entries because your region is only one pixel wide. I don't think there's a standard method that handles your special problem directly. However, you can simply use the unique() function to eliminate double entries of the result.
To keep the original order of the points, just do:
b = bwboundaries(A);
[dummy, ind] = unique(b{1}, 'rows', 'first');
contour = b{1}(sort(ind), :);