Let A be an matrix of size [n,n]. If I want to extract its diagonal, I do diag(A).
Actually, I want the opposite diagonal, which would be [A(n,1),A(n-1,2),A(n-2,3),...].
One way to do this is via diag(flipud(A)). However, flipud(A) is quite wasteful and multiplies the time it takes by a factor of 10 compared to finding the usual diagonal.
I'm looking for a fast way of obtaining the opposite diagonal. Naturally, for loops seem abysmally slow. Suggestions would be greatly appreciated.
Here is my matrix, produced by A = magic(5)
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
s = size(A,1)
A(s:s-1:end-1)
ans =
11 12 13 14 15
Below is a comparison of all the methods mentioned so far, plus a few other variations I could think of. This was tested on 64-bit R2013a using TIMEIT function.
function [t,v] = testAntiDiag()
% data and functions
A = magic(5000);
f = {
#() func0(A) ;
#() func1(A) ;
#() func2(A) ;
#() func3(A) ;
#() func4(A) ;
#() func5(A) ;
#() func6(A) ;
#() func7(A) ;
};
% timeit and check results
t = cellfun(#timeit, f, 'UniformOutput',true);
v = cellfun(#feval, f, 'UniformOutput',false);
assert( isequal(v{:}) )
end
function d = func0(A)
d = diag(A(end:-1:1,:));
end
function d = func1(A)
d = diag(flipud(A));
end
function d = func2(A)
d = flipud(diag(fliplr(A)));
end
function d = func3(A)
d = diag(rot90(A,3));
end
function d = func4(A)
n = size(A,1);
d = A(n:n-1:end-1).';
end
function d = func5(A)
n = size(A,1);
d = A(cumsum(n + [0,repmat(-1,1,n-1)])).';
end
function d = func6(A)
n = size(A,1);
d = A(sub2ind([n n], n:-1:1, 1:n)).';
end
function d = func7(A)
n = size(A,1);
d = zeros(n,1);
for i=1:n
d(i) = A(n-i+1,i);
end
end
The timings (in the same order they are defined above):
>> testAntiDiag
ans =
0.078635867152801
0.077895631970976 % #AlexR.
0.080368641824528
0.195832501156751
0.000074983294297 % #thefourtheye
0.000143019460665 % #woodchips
0.000174679680437
0.000152488508547 % for-loop
The most suprising result to me is the last one. Apparently JIT compilation is very effective on such simple for-loops.
The elements you want are easily obtained by indexing. For example, this should do the trick.
n = 4;
A = magic(n)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
A(cumsum(n + [0,repmat(-1,1,n-1)]))
ans =
4 7 10 13
I could also have used sub2ind to get those element indexes, but this does it a bit more cleanly, though less obvious in how it works.
A = magic(6)
A =
35 1 6 26 19 24
3 32 7 21 23 25
31 9 2 22 27 20
8 28 33 17 10 15
30 5 34 12 14 16
4 36 29 13 18 11
b = diag(A(1:length(A),length(A):-1:1))
b =
24
23
22
33
5
4
Related
Context:
I have a hydraulic erosion algorithm that needs to receive an array of droplet starting positions. I also already have a pattern replicating algorithm, so I only need a good pattern to replicate.
The Requirements:
I need an algorism that produces a set of n^2 entries in a set of format (x,y) or [index] that describe cells in an nxn grid (where n = 2^i where i is any positive integer).
(as a set it means that every cell is mentioned in exactly one entry)
The pattern [created by the algorism ] should contain zero to none clustering of "visited" cells at any stage.
The cell (0,0) is as close to (n-1,n-1) as to (1,1), this relates to the definition of clustering
Note
I was/am trying to find solutions through fractal-like patterns built through recursion, but at the time of writing this, my solution is a lookup table of a checkerboard pattern(list of black cells + list of white cells) (which is bad, but yields fewer artifacts than an ordered list)
C, C++, C#, Java implementations (if any) are preferred
You can use a linear congruential generator to create an even distribution across your n×n space. For example, if you have a 64×64 grid, using a stride of 47 will create the pattern on the left below. (Run on jsbin) The cells are visited from light to dark.
That pattern does not cluster, but it is rather uniform. It uses a simple row-wide transformation where
k = (k + 47) mod (n * n)
x = k mod n
y = k div n
You can add a bit of randomness by making k the index of a space-filling curve such as the Hilbert curve. This will yield the pattern on the right. (Run on jsbin)
You can see the code in the jsbin links.
I have solved the problem myself and just sharing my solution:
here are my outputs for the i between 0 and 3:
power: 0
ordering:
0
matrix visit order:
0
power: 1
ordering:
0 3 2 1
matrix visit order:
0 3
2 1
power: 2
ordering:
0 10 8 2 5 15 13 7 4 14 12 6 1 11 9 3
matrix visit order:
0 12 3 15
8 4 11 7
2 14 1 13
10 6 9 5
power: 3
ordering:
0 36 32 4 18 54 50 22 16 52 48 20 2 38 34 6
9 45 41 13 27 63 59 31 25 61 57 29 11 47 43 15
8 44 40 12 26 62 58 30 24 60 56 28 10 46 42 14
1 37 33 5 19 55 51 23 17 53 49 21 3 39 35 7
matrix visit order:
0 48 12 60 3 51 15 63
32 16 44 28 35 19 47 31
8 56 4 52 11 59 7 55
40 24 36 20 43 27 39 23
2 50 14 62 1 49 13 61
34 18 46 30 33 17 45 29
10 58 6 54 9 57 5 53
42 26 38 22 41 25 37 21
the code:
public static int[] GetPattern(int power, int maxReturnSize = int.MaxValue)
{
int sideLength = 1 << power;
int cellsNumber = sideLength * sideLength;
int[] ret = new int[cellsNumber];
for ( int i = 0 ; i < cellsNumber && i < maxReturnSize ; i++ ) {
// this loop's body can be used for per-request computation
int x = 0;
int y = 0;
for ( int p = power - 1 ; p >= 0 ; p-- ) {
int temp = (i >> (p * 2)) % 4; //2 bits of the index starting from the begining
int a = temp % 2; // the first bit
int b = temp >> 1; // the second bit
x += a << power - 1 - p;
y += (a ^ b) << power - 1 - p;// ^ is XOR
// 00=>(0,0), 01 =>(1,1) 10 =>(0,1) 11 =>(1,0) scaled to 2^p where 0<=p
}
//to index
int index = y * sideLength + x;
ret[i] = index;
}
return ret;
}
I do admit that somewhere along the way the values got transposed, but it does not matter because of how it works.
After doing some optimization I came up with this loop body:
int x = 0;
int y = 0;
for ( int p = 0 ; p < power ; p++ ) {
int temp = ( i >> ( p * 2 ) ) & 3;
int a = temp & 1;
int b = temp >> 1;
x = ( x << 1 ) | a;
y = ( y << 1 ) | ( a ^ b );
}
int index = y * sideLength + x;
(the code assumes that c# optimizer, IL2CPP, and CPP compiler will optimize variables temp, a, b out)
I had this as a test question to evaluate the time complexity of the below recursive method.
def multiply(x,y)
if y = 0:
return 0
z = multiply(x,y/2)
if y is even:
return 2z
else :
return x + 2z
I had written log(n) as the number y keeps decreasing by 2 and the recursive call will soon end as it meets the condition.
If the function is meant to have a zero check as stated above in the question, this function will never terminate.
Here is a working python version:
def multiply(x,y):
if y == 0:
return 0
z = multiply(x,y/2)
if y % 2 == 0:
return 2 * z
else :
return x + 2 * z
Whatever you feed into it: this will end in a recursion error. So complexity is infinite. Perhaps this is a trick question.
If however what your teacher meant was something like this:
def multiply(x, y):
if 0.01 > y > -0.01:
return 0
z = multiply(x, y / 2)
if y % 2 == 0:
return 2 * z
else:
return x + 2 * z
then complexity looks indeed like log(n).
Here is some complexity benchmarking code which counts the number of operations:
counter = 0
def multiply(x, y):
global counter
counter += 1
if 0.01 > y > -0.01:
return 0
z = multiply(x, y / 2)
if y % 2 == 0:
return 2 * z
else:
return x + 2 * z
for i in range(1000):
counter = 0
multiply(1, i)
print(i, counter)
It prints a sequence of numbers which is logarithmic in nature:
0 1
1 8
2 9
3 10
4 10
5 10
6 11
7 11
8 11
9 11
10 11
11 12
12 12
13 12
14 12
15 12
16 12
17 12
18 12
19 12
20 12
21 13
22 13
23 13
24 13
25 13
26 13
27 13
28 13
29 13
30 13
...
Consider we have N points on a circle. To each point an index is assigned i = (1,2,...,N). Now, for a randomly selected point, I want to have a vector including the indices of 5 points, [two left neighbors, the point itself, two right neighbors].
See the figure below.
Some sxamples are as follows:
N = 18;
selectedPointIdx = 4;
sequence = [2 3 4 5 6];
selectedPointIdx = 1
sequence = [17 18 1 2 3]
selectedPointIdx = 17
sequence = [15 16 17 18 1];
The conventional way to code this is considering the exceptions as if-else statements, as I did:
if ii == 1
lseq = [N-1 N ii ii+1 ii+2];
elseif ii == 2
lseq = [N ii-1 ii ii+1 ii+2];
elseif ii == N-1
lseq=[ii-2 ii-1 ii N 1];
elseif ii == N
lseq=[ii-2 ii-1 ii 1 2];
else
lseq=[ii-2 ii-1 ii ii+1 ii+2];
end
where ii is selectedPointIdx.
It is not efficient if I consider for instance 7 points instead of 5. What is a more efficient way?
How about this -
off = -2:2
out = mod((off + selectedPointIdx) + 17,18) + 1
For a window size of 7, edit off to -3:3.
It uses the strategy of subtracting 1 + modding + adding back 1 as also discussed here.
Sample run -
>> off = -2:2;
for selectedPointIdx = 1:18
disp(['For selectedPointIdx =',num2str(selectedPointIdx),' :'])
disp(mod((off + selectedPointIdx) + 17,18) + 1)
end
For selectedPointIdx =1 :
17 18 1 2 3
For selectedPointIdx =2 :
18 1 2 3 4
For selectedPointIdx =3 :
1 2 3 4 5
For selectedPointIdx =4 :
2 3 4 5 6
For selectedPointIdx =5 :
3 4 5 6 7
For selectedPointIdx =6 :
4 5 6 7 8
....
For selectedPointIdx =11 :
9 10 11 12 13
For selectedPointIdx =12 :
10 11 12 13 14
For selectedPointIdx =13 :
11 12 13 14 15
For selectedPointIdx =14 :
12 13 14 15 16
For selectedPointIdx =15 :
13 14 15 16 17
For selectedPointIdx =16 :
14 15 16 17 18
For selectedPointIdx =17 :
15 16 17 18 1
For selectedPointIdx =18 :
16 17 18 1 2
You can use modular arithmetic instead: Let p be the point among N points numbered 1 to N. Say you want m neighbors on each side, you can get them as follows:
(p - m - 1) mod N + 1
...
(p - 4) mod N + 1
(p - 3) mod N + 1
(p - 2) mod N + 1
p
(p + 1) mod N + 1
(p + 2) mod N + 1
(p + 3) mod N + 1
...
(p + m - 1) mod N + 1
Code:
N = 18;
p = 2;
m = 3;
for i = p - m : p + m
nb = mod((i - 1) , N) + 1;
disp(nb);
end
Run code here
I would like you to note that you might not necessarily improve performance by avoiding a if statement. A benchmark might be necessary to figure this out. However, this will only be significant if you are treating tens of thousands of numbers.
I want to map a mX1 matrix X into mXp matrix Y where each row in the new matrix is as follows:
Y = [ X X.^2 X.^3 ..... X.^p]
I tried to use the following code:
Y = zeros(m, p);
for i=1:m
Y(i,:) = X(i);
for c=2:p
Y(i,:) = [Y(i,:) X(i).^p];
end
end
What you want do is called brodcasting. If you are using Octave 3.8 or later, the following will work fine:
octave> X = (1:5)'
X =
1
2
3
4
5
octave> P = (1:5)
P =
1 2 3 4 5
octave> X .^ P
ans =
1 1 1 1 1
2 4 8 16 32
3 9 27 81 243
4 16 64 256 1024
5 25 125 625 3125
The important thing to note is how X and P are a column and row vector respectively. See the octave manual on the topic.
For older of versions of Octave (without automatic broadcasting), the same can be accomplished with bsxfun (#power, X, P)
I have a column vector that needs to be changed into a matrix. The size of matrix is specified and can change. Please suggest a vectorized solution.
rows = 3 ; cols = 4 ; %matrix elements for this case = 12
colvector = [ 2;4;5;8;10;14;16;18;20;21;28;30] ;
desired_mat = [ ...
2 4 5 8
10 14 16 18
20 21 28 30 ] ;
Thanks!
The reshape function does that:
>> colvector = [ 2;4;5;8;10;14;16;18;20;21;28;30] ;
>> A = reshape(colvector, 3, 4)
A =
2 8 16 21
4 10 18 28
5 14 20 30