How can I avoid using a double for loop in order to build a matrix pos like this code does:
pos=[0 0];
for i=1:m;
for j=1:n;
pos=[pos; i j];
end
end
m and n are numbers such as 500 and 900.
I have to find a better solution in order improve computation time.
Thank you so much.
You can easily do this by meshgrid.
[X,Y] = meshgrid(1:m, 1:n);
pos = [0 0; X(:) Y(:)];
How the above code works is the following. meshgrid (in this case) creates a 2D grid of (X,Y) co-ordinates. X progresses horizontally while Y progresses vertically. As we can see in your for loops, m defines the horizontal boundaries while n denotes the vertical boundaries. By calling meshgrid(1:m, 1:n), I am creating a n x m grid for both X and Y, where each row of X progresses from 1 to m, while each column of Y progresses from 1 to n. Therefore, these will both be n x m matrices. Calling the above with m = 4 and n = 5 computes:
m = 4;
n = 5;
[X,Y] = meshgrid(1:m, 1:n)
X =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Y =
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
This almost follows the format you wish. You'll notice that by looking at the columns individually, this achieves what you want, but you want to stack all of the X and Y to be in a (n x m) + 1 x 2 matrix (1 to account for [0 0]). All we have to do now is take every column of X and Y and stack them on top of each other to create a single column for both. We can stack all of these together by doing X(:) and Y(:). X(:) will take every column of X and create a single column that stacks all of the columns together. The same is done for Y(:). As such, we first create pos by attaching [0 0] as the first row, and we then attach X(:) and Y(:) as columns to pos after, thus completing the construction of pos.
Let's do an example as a proof-of-concept. Suppose that we use the same values like we did before:
m = 4;
n = 5;
Using your for loop, we get:
pos =
0 0
1 1
1 2
1 3
1 4
1 5
2 1
2 2
2 3
2 4
2 5
3 1
3 2
3 3
3 4
3 5
4 1
4 2
4 3
4 4
4 5
Using the code I have written, we also get:
pos =
0 0
1 1
1 2
1 3
1 4
1 5
2 1
2 2
2 3
2 4
2 5
3 1
3 2
3 3
3 4
3 5
4 1
4 2
4 3
4 4
4 5
Minor Note
As you stated that m and n are going to be relatively large, I would recommend you clear X and Y from your workspace before you proceed. X and Y were only created to help you create pos. As you don't need them anymore, after you calculate pos, do:
clear X;
clear Y;
Related
How can I generate the following matrix having m rows and n columns?
1st row 1 2 3 L n-1 n
2nd 2 3 4 L n 1
3rd 3 4 5 n-1 1 2
Nth M M M ....
N-1 m-1 m m+1 L m-3 m-2
last m m+1 m+2 l m-2 m-1
It's difficult to tell from your description, but it appears you want to create a matrix where the first row is 1:n and each successive row is a circular shift to the left of the previous row. If so, you can still use hankel for this (as Dev-iL mentions in their answer), but incorporate a remainder operation like so:
n = 5;
m = 8;
mat = rem(hankel(0:(m-1), (m-1):(m+n-2)), n)+1
mat =
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
This looks like a Hankel matrix. You should use the 2-input syntax for it,
H = hankel(c,r)
So for example, with m = 4 and n = 5 we get:
m = 4; n = 5;
X = hankel( 1:m, m:m+n-1 )
X =
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
How can I extend a matrix in MATLAB by symmetrically replicating the boundary values? For example, if X is my matrix, extended matrix Xextsym should look like the following:
X =
1 2 3
4 5 6
Xextsym =
5 4 4 5 6 6 5
2 1 1 2 3 3 2
2 1 1 2 3 3 2
5 4 4 5 6 6 5
5 4 4 5 6 6 5
2 1 1 2 3 3 2
I'm aware that a function called wextend exists in the Wavelet Toolbox for this task exactly, but I don't have it.
Here is a function that extends a given matrix A by symmetric reflection, adding w element in each of four directions. Usage example:
symextend([1 2 3; 4 5 6], 2)
returns the extended matrix in your question.
function R = symextend(A, w)
[m, n] = size(A);
B = [A A(:, n:-1:1); A(m:-1:1, :) A(m:-1:1, n:-1:1)];
repm = 2*ceil(0.5+0.5*w/m);
repn = 2*ceil(0.5+0.5*w/n);
C = repmat(B, repm, repn);
R = C(m*repm + 1 - w : m*repm + m + w, n*repn + 1 - w : n*repn + n + w)
end
Idea: first reflect A once in each direction (this produces B), then repeat B with repmat, obtaining C. Finally, a suitable piece is carved out of C. The tricky parts are counting how many times to repeat, and which part to carve out.
Supose there is a Matrix
A =
1 3 2 4
4 2 5 8
6 1 4 9
and I have a Vector containing the "class" of each column of this matrix for example
v = [1 , 1 , 2 , 3]
How can I sum the columns of the matrix to a new matrix as column vectors each to the column of their class? In this example columns 1 and 2 of A would added to the first column of the new matrix, column 2 to the 3 to the 2nd, column 4 the the 3rd.
Like
SUM =
4 2 4
6 5 8
7 4 9
Is this possible without loops?
One of the perfect scenarios to combine the powers of accumarray and bsxfun -
%// Since we are to accumulate columns, first step would be to transpose A
At = A.' %//'
%// Create a vector of linear IDs for use with ACCUMARRAY later on
idx = bsxfun(#plus,v(:),[0:size(A,1)-1]*max(v))
%// Use ACCUMARRAY to accumulate rows from At, i.e. columns from A based on the IDs
out = reshape(accumarray(idx(:),At(:)),[],size(A,1)).'
Sample run -
A =
1 3 2 4 6 0
4 2 5 8 9 2
6 1 4 9 8 9
v =
1 1 2 3 3 2
out =
4 2 10
6 7 17
7 13 17
An alternative with accumarray in 2D. Generate a grid with the vector v and then apply accumarray:
A = A.';
v = [1 1 2 3];
[X, Y] = ndgrid(v,1:size(A,2));
Here X and Y look like this:
X =
1 1 1
1 1 1
2 2 2
3 3 3
Y =
1 2 3
1 2 3
1 2 3
1 2 3
Then apply accumarray:
B=accumarray([X(:) Y(:)],A(:)),
SUM = B.'
SUM =
4 2 4
6 5 8
7 4 9
As you see, using [X(:) Y(:)] create the following array:
ans =
1 1
1 1
2 1
3 1
1 2
1 2
2 2
3 2
1 3
1 3
2 3
3 3
in which the vector v containing the "class" is replicated 3 times since there are 3 unique classes that are to be summed up together.
EDIT:
As pointed out by knedlsepp you can get rid of the transpose to A and B like so:
[X2, Y2] = ndgrid(1:size(A,1),v);
B = accumarray([X2(:) Y2(:)],A(:))
which ends up doing the same. I find it a bit more easier to visualize with the transposes but that gives the same result.
How about a one-liner?
result = full(sparse(repmat(v,size(A,1),1), repmat((1:size(A,1)).',1,size(A,2)), A));
Don't optimize prematurely!
The for loop performs fine for your problem:
out = zeros(size(A,1), max(v));
for i = 1:numel(v)
out(:,v(i)) = out(:,v(i)) + A(:,i);
end
BTW: With fine, I mean: fast, fast, fast!
In Matlab I have a big matrix containing the coordinates (x,y,z) of many points (over 200000). There is an extra column used as identification. I have written this code in order to sort all coordinate points. My final goal is to find duplicated points (rows with same x,y,z). After sorting the coordinate points I use the diff function, two consecutive rows of the matrix with the same coordinates will take value [0 0 0], and then with ismember I can find which rows of that matrix resulting from applying "diff" have the [0 0 0] row. With the indices returned from ismember I can find which points are repeated.
Back to my question...This is the code I wrote to sort properly my coordintes+id matrix. I guess It could be done better. Any suggestion?
%coordinates are always positive
a=[ 1 2 8 4; %sample matrix
1 0 5 6;
2 4 7 1;
3 2 1 0;
2 3 5 0;
3 1 2 8;
1 2 4 8];
b=a; %for checking purposes
%sorting first column
a=sortrows(a,1);
%sorting second column
for i=0:max(a(:,1));
k=find(a(:,1)==i);
if not(isempty(k))
a(k,:)=sortrows(a(k,:),2);
end
end
%Sorting third column
for i=0:max(a(:,2));
k=find(a(:,2)==i);
if not(isempty(k))
%identifying rows with same value on first column
for j=1:length(k)
[rows,~] = ismember(a(:,1:2), [ a(k(j),1),i],'rows');
a(rows,3:end)=sortrows(a(rows,3:end),1);
end
end
end
%Checking that rows remain the same
m=ismember(b,a,'rows');
if length(m)~=sum(m)
disp('Error while sorting!');
end
Why don't you just use unique?
[uniqueRows, ii, jj] = unique(a(:,1:3),'rows');
Example
a = [1 2 3 5
3 2 3 6
1 2 3 9
2 2 2 8];
gives
uniqueRows =
1 2 3
2 2 2
3 2 3
and
jj =
1
3
1
2
meaning third row equals first row.
If you need the full unique rows, including the fourth column: use ii to index a:
fullUniqueRows = a(ii,:);
which gives
fullUniqueRows =
1 2 3 9
2 2 2 8
3 2 3 6
Trying to sort a based on the fourth column? Do this -
a=[ 1 2 8 4; %sample matrix
1 0 5 6;
2 4 7 1;
3 2 1 0;
2 3 5 0;
3 2 1 8;
1 2 4 8];
[x,y] = sort(a(:,4))
sorted_a=a(y,:)
Trying to get the row indices having repeated x-y-z coordinates being represented by the first three columns? Do this -
out = sum(squeeze(all(bsxfun(#eq,a(:,1:3),permute(a(:,1:3),[3 2 1])),2)),2)>1
and use it similarly for sorted_a.
e.g I have the original matrix (m) looks like this one
1 2
3 4
Then I use n = padarray(m,[oldMatrixRow,OldMatrixColumn]); I will have
x x x x x x
x x x x x x
x x 1 2 x x
x x 3 4 x x
x x x x x x
x x x x x x
The point here is that I would want my new matrix look like this
1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4
Is there any smart way to do that ?
Thank you very much
You want repmat
B = repmat(A,m,n)
where A is the matrix you want to repeat, and m and n define the dimensions of how it is repeated
In your case here, call your original matrix A and use
B = repmat(A,3,3)
to get your desired output
Assuming you just want to repeat your matrix a number of times the easy way would be to use repmat:
m = [1 2;3 4];
n = repmat(m,3,3)
Just to give an alternative solution:
kron(ones(3), [1 2; 3 4])