I have a p-by-p-by-n tensor. I want to extract diagonal element for each p-by-p slice. Are there anyone know how to do this without looping?
Thank you.
Behold the ever mighty and ever powerful bsxfun for vectorizing MATLAB problems to do this task very efficiently using MATLAB's linear indexing -
diags = A(bsxfun(#plus,[1:p+1:p*p]',[0:n-1]*p*p))
Sample run with 4 x 4 x 3 sized input array -
A(:,:,1) =
0.7094 0.6551 0.9597 0.7513
0.7547 0.1626 0.3404 0.2551
0.2760 0.1190 0.5853 0.5060
0.6797 0.4984 0.2238 0.6991
A(:,:,2) =
0.8909 0.1493 0.8143 0.1966
0.9593 0.2575 0.2435 0.2511
0.5472 0.8407 0.9293 0.6160
0.1386 0.2543 0.3500 0.4733
A(:,:,3) =
0.3517 0.9172 0.3804 0.5308
0.8308 0.2858 0.5678 0.7792
0.5853 0.7572 0.0759 0.9340
0.5497 0.7537 0.0540 0.1299
diags =
0.7094 0.8909 0.3517
0.1626 0.2575 0.2858
0.5853 0.9293 0.0759
0.6991 0.4733 0.1299
Benchmarking
Here's few runtime tests comparing this bsxfun based approach against repmat + eye based approach for big datasizes -
***** Datasize: 500 x 500 x 500 *****
----------------------- With BSXFUN
Elapsed time is 0.008383 seconds.
----------------------- With REPMAT + EYE
Elapsed time is 0.163341 seconds.
***** Datasize: 800 x 800 x 500 *****
----------------------- With BSXFUN
Elapsed time is 0.012977 seconds.
----------------------- With REPMAT + EYE
Elapsed time is 0.402111 seconds.
***** Datasize: 1000 x 1000 x 500 *****
----------------------- With BSXFUN
Elapsed time is 0.017058 seconds.
----------------------- With REPMAT + EYE
Elapsed time is 0.690199 seconds.
One suggestion I have is to create a p x p logical identity matrix, replicate this n times in the third dimension, and then use this matrix to access your tensor. Something like this, supposing that your tensor was stored in A:
ind = repmat(logical(eye(p)), [1 1 n]);
out = A(ind);
Example use:
>> p = 5; n = 3;
>> A = reshape(1:75, p, p, n)
A(:,:,1) =
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25
A(:,:,2) =
26 31 36 41 46
27 32 37 42 47
28 33 38 43 48
29 34 39 44 49
30 35 40 45 50
A(:,:,3) =
51 56 61 66 71
52 57 62 67 72
53 58 63 68 73
54 59 64 69 74
55 60 65 70 75
>> ind = repmat(logical(eye(p)), [1 1 n]);
>> out = A(ind)
out =
1
7
13
19
25
26
32
38
44
50
51
57
63
69
75
You'll notice that we grab the diagonals of the first slice, followed by the diagonals of the second slice, etc. up until the last slice. These values are all concatenated into a single vector.
Just reading Divakar's answer and trying to understand why he again is roughly 10 times faster than my idea I put together code mixing both, and ended up with code which is faster:
A=reshape(A,[],n);
diags2 = A(1:p+1:p*p,:);
For a 500x500x500 tensor I get 0.008s for Divakar's solution and 0.005s for my solution using Matlab 2013a. Probably plain indexing is the only way to beat bsxfun.
Related
I have a file named Myfile.txt with a header and three rows of data:
Header Row1 Row2 Row3
5.10 10 15
5.20 20 25
5.30 30 35
5.40 40 45
5.50 50 55
5.60 60 65
5.70 70 75
5.80 80 85
5.90 90 95
5.95 10 20
6.00 25 30
6.05 35 40
I want to read in every 3 lines that increment the first column by .1 and then all lines after the increment to .05, so my output looks like:
5.30 30 35
5.60 60 65
5.90 90 95
5.95 10 20
6.00 25 30
6.05 35 40
I have the following code but I don't know how to implement the condition, can I get some help doing this?
per_line = 3;
every_nth_line = 3;
fmt = [repmat('%*f',1,per_line*(every_nth_line-1)), repmat('%f',1,per_line)];
fid = fopen('Myfile.txt','rt');
datacell = textscan(fid,fmt,'delimiter','\n','HeaderLines',1,'CollectOutput',1);
fclose(fid);
C=datacell{1};
You can use the following code:
fileID = fopen('Myfile.txt');
mydata = textscan(fileID,'%f%f%f','HeaderLines',2);
findx = (find(abs(diff([mydata{1}(1)-0.1;mydata{1}])-0.1000)<0.0001));
sindx = (find(abs(diff(mydata{1})-0.05)<0.0001))+1;
alldata = [mydata{:}];
C= [alldata(findx(3:3:end),:);alldata(sindx,:)];
fclose(fileID);
diff is used to determine the difference between entries in the first column. abs is used to determine equality between floating point numbers within a small difference, and find is used to return their indexes.
C contains:
5.3000 30.0000 35.0000
5.6000 60.0000 65.0000
5.9000 90.0000 95.0000
5.9500 10.0000 20.0000
6.0000 25.0000 30.0000
6.0500 35.0000 40.0000
I tried to resample my data from a block of matrix that defined its indices. Hopefully this example can make it clear:
A=rand(18400,100);
A_IDX=randi([1 100],[18400 100]);
A_IDX consist 18400 rows and 100 columns. I wanted to extract the matrix A at the A_IDX indices. Result would be something like:
A=[1 2 3; 4 5 6];
A_IDX=[1 3; 2 3];
A_Result=[1 3; 5 6];
I tried A(:,A_IDX) but that gave me 1840x184000 matrix size, which is not what I wanted to do in the first place. Anyone can help? Thanks in advance!
We could get the linear index equivalent for those indices and then simply indexing into the input array would give us the desired output. Now, to get those linear indices, we would make use of bsxfun for the math computations related to the index computations, which would basically involve scaling and offsetting.
Indexing with 2D array of column indices
For a 2D array of column indices, we would have -
function out = take_cols(a, col_idx)
n = size(a,1);
lidx = bsxfun(#plus,(col_idx-1)*n,(1:n).');
out = a(lidx);
Sample run -
>> a
a =
39 83 39 48 36
58 74 20 19 50
69 97 65 34 57
47 58 80 24 51
>> col_idx
col_idx =
2 4
3 5
1 4
2 5
>> take_cols(a, col_idx)
ans =
83 48
20 50
69 34
58 51
Indexing with 2D array of row indices
For a 2D array of row indices, it would be -
function out = take_rows(a, row_idx)
[m,n] = size(a);
lidx = bsxfun(#plus,row_idx, (0:n-1)*m);
out = a(lidx);
Sample run -
>> a
a =
39 83 39 48 36
58 74 20 19 50
69 97 65 34 57
47 58 80 24 51
>> row_idx
row_idx =
3 2 3 1 2
4 3 4 2 4
>> take_rows(a, row_idx)
ans =
69 74 65 48 50
47 97 80 19 51
This weird monster of code will give you what you want. It generates proper subscripts for each index and converts them to linear, then just indexes A linearly.
A_IDX_aux=A_IDX';
reshape(A(sub2ind(size(A),repelem(1:size(A,1),1,size(A_IDX,1)).',A_IDX_aux(:))),[size(A,1), size(A_IDX,2)]).';
I find my solution for this task too, but not so fast, as Divakar and Ander :)
Behold:
res = cell2mat(arrayfun( #(x) A(x,A_IDX(x,:)), (1:size(A,1))', 'UniformOutput',false));
It use cell2mat and I suppose it is not so fast as bsxfun, but hope is still alive and I was curios to test all the 3 solutions. And I got unobvious results!
Elapsed time is 0.000058 seconds. % Divakar
Elapsed time is 0.000077 seconds. % Andres
Elapsed time is 0.000339 seconds. % Me
This mean bsxf is fastest! But using right indexing give fast result too! And my solution was really slow. I suppose it's because of 'UniformOutput', false - I forced to convert to cells and then back, so it slow my method a lot.
Conclusion:
If you can use bsxf - use it!
Despite the fact that my method looks more visually pleasing than that of Andres, it is still slower.
So there is no any sense to post this answer :D I spend some time for current work, maybe it will help someone in future
I would like to remove the diagonal of the following matrix;
[0 1 1
0 0 0
0 1 0]
and put this in a vector as such
[1 1 0 0 0 1]
Is there a one-way function to do this?
Most other solutions I found on Stack Overflow delete all zeros.
If two lines are fine...
x = x.'; %'// transpose because you want to work along 2nd dimension first
result = x(~eye(size(x))).'; %'// index with logical mask to remove diagonal
Here's an almost one-liner -
[m,n] = size(x);
x(setdiff(reshape(reshape(1:numel(x),m,n).',1,[]),1:m+1:numel(x),'stable'))
And I will put up my fav bsxfun here -
xt = x.'; %//'
[m,n] = size(x);
out = xt(bsxfun(#ne,(1:n)',1:m)).'
Sample run -
>> x
x =
52 62 37 88
23 68 98 91
49 40 4 79
>> [m,n] = size(x);
>> x(setdiff(reshape(reshape(1:numel(x),m,n).',1,[]),1:m+1:numel(x),'stable'))
ans =
62 37 88 23 98 91 49 40 79
>> xt = x.';
>> xt(bsxfun(#ne,(1:n)',1:m)).'
ans =
62 37 88 23 98 91 49 40 79
I have an MxN matrix and I want a column vector v, using the vector s that tells me for each row in the matrix what column I will take.
Here's an example:
Matrix =
[ 4 13 93 20 42;
31 18 94 64 02;
7 44 24 91 15;
11 20 43 38 31;
21 42 72 60 99;
13 81 31 87 50;
32 22 83 24 04]
s = [4 4 5 4 4 4 3].'
And the desired output is:
v = [20 64 15 38 60 87 83].'
I thought using the expression
Matrix(:,s)
would've work but it doesn't. Is there a solution without using for loops to access the rows separately?
It's not pretty, and there might be better solutions, but you can use the function sub2ind like this:
M(sub2ind(size(M),1:numel(s),s'))
You can also do it with linear indexing, here is an example:
M=M'; s=s';
M([0:size(M,1):numel(M)-1]+s)
Assume we have the following data:
H_T = [36 66 21 65 52 67 73; 31 23 19 33 36 39 42]
P = [40 38 39 40 35 32 37]
Using MATLAB 7.0, I want to create three new matrices that have the following properties:
The matrix H (the first part in matrix H_T) will be divided to 3 intervals:
Matrix 1: the 1st interval contains the H values between 20 to 40
Matrix 2: the 2nd interval contains the H values between 40 to 60
Matrix 3: the 3rd interval contains the H values between 60 to 80
The important thing is that the corresponding T and P will also be included in their new matrices meaning that H will control the new matrices depending on the specifications defined above.
So, the resultant matrices will be:
H_T_1 = [36 21; 31 19]
P_1 = [40 39]
H_T_2 = [52; 36]
P_2 = [35]
H_T_3 = [66 65 67 73; 23 33 39 42]
P_3 = [38 40 32 37]
Actually, this is a simple example and it is easy by looking to create the new matrices depending on the specifications, BUT in my values I have thousands of numbers which makes it very difficult to do that.
Here's a quick solution
[~,bins] = histc(H_T(1,:), [20 40 60 80]);
outHT = cell(3,1);
outP = cell(3,1);
for i=1:3
idx = (bins == i);
outHT{i} = H_T(:,idx);
outP{i} = P(idx);
end
then you access the matrices as:
>> outHT{3}
ans =
66 65 67 73
23 33 39 42
>> outP{3}
ans =
38 40 32 37