Extracting lower Triangular matrix without considering diagonal element - matlab

I have a 5x5 matrix
A =
[0 0 0 0 1;
0.36 0 0 0 1;
0 0.25 0 0 1;
0.35 0 0 0 1;
0 0 0.28 0 1];
I want to extract lower triangular elements of the matrix without considering the diagonal elements. Therefore, resulting matrix should be
C = [0.36 0 0.35 0 0.25 0 0 0 0.28 0]
Let me know how I can get this.

You can use matlab tril, e.g.:
index = find(tril(ones(size(A)), -1));
C = A(index);

I'm assuming this is Matlab, based on the format of the matrices.
If you want the elements of the lower triangular portion in a row vector, you can do it with a loop (although I'm sure somebody will have a nifty vectorized approach):
C=[];
for n=1:size(A,1)
C=[C,A(n+1:end,n)'];
end
The output is:
C =
0.36 0.00 0.35 0.00 0.25 0.00 0.00 0.00 0.28 0.00
If instead you want the full 5x5 matrix, you would use the second argument of tril:
C = tril(A,-1);
The output is:
C =
0.00 0.00 0.00 0.00 0.00
0.36 0.00 0.00 0.00 0.00
0.00 0.25 0.00 0.00 0.00
0.35 0.00 0.00 0.00 0.00
0.00 0.00 0.28 0.00 0.00

bsxfun-based approach:
C = A( bsxfun(#gt, (1:size(A,1)).', 1:size(A,2)) ).';

Related

Number of combinations in Matlab is missing

I am creating a list of combinations of 3 coefficients. The sum is 1 and the increment for each coefficient is 0.05:
s=0;
for x1 = 0 : 0.05 : 1
for x2 = 0 : 0.05 : (1-x1)
x3 = 1-x1-x2;
s=s+1;
fprintf('%0.2f %0.2f %0.2f \n',x1,x2,x3)
end
end
The number expected is 231 but I got 229.
Checking the result:
...
0.85 0.00 0.15
0.85 0.05 0.10
0.85 0.10 0.05
0.90 0.00 0.10
0.90 0.05 0.05
0.90 0.10 -0.00
0.95 0.00 0.05
1.00 0.00 0.00
I found '0.85 0.15 0.00' and '0.95 0.05 0.00' were missing but I can't explain why. Could anyone give me some hint about this. Much appreciate for your time.
You’re asking for hints, so I’ll give you some.
Read this Q&A: Why is 24.0000 not equal to 24.0000 in MATLAB?. Now think about what the upper limit for x2 (1-x1) exactly is and how you get there by incrementing 0 in steps of 0.05.

sum specific columns in matlab vectorized

I have a big matrix lets say 100000x13.
I need to sum specific columns of this matrix.
For example:
matrix = [0.70 0.30 0 0 0.15 0.21 0.58 0.06 0.00 1.00 0 0 1.00;
0.70 0.00 0 0 0.00 0.00 0.07 0.06 0.00 0.80 0 0 1.00;
0.70 0.00 0 0 0.00 0.00 0.58 0.06 0.00 1.00 0 0 0.94];
inputVect = [4 4 3 2];
idx2 = cumsum(inputVect);
idx1 = [1 idx2(1:end-1)+1];
result = (1-sum(matrix(:,idx1(1):idx2(1)),2)) + (1-sum(matrix(:,idx1(2):idx2(2)),2))+(1-sum(matrix(:,idx1(3):idx2(3)),2)) + (1-sum(matrix(:,idx1(4):idx2(4)),2));
The thing is inputVect needs to be a function argument that I don't allways know the size. And because of the size of the matrix I should also avoid for loops. Any help would be much appreciated.

How can I find neighbors of spesific value in a matrix via Matlab?

I have a 256*256 matrix, some values are 0 (close the each other); and I find the coordinates' of 0 values.
% finding missing rows and cols: xi, yi
[row,col]=find(~X);
MIS=[row,col];
MISWO=[MIS zeros(size(MIS,1),1) ];
MISWO
...
168 224 0
169 224 0
170 224 0
171 224 0
172 224 0
173 224 0
174 224 0
Part of the X matrix:
0.57 0.58 0.00 0.55 0.54
0.55 0.54 0.00 0.55 0.52
0.56 0.55 0.00 0.55 0.53
0.56 0.55 0.00 0.53 0.52
0.56 0.00 0.00 0.53 0.54
0.55 0.00 0.00 0.53 0.52
0.55 0.00 0.00 0.55 0.51
0.55 0.00 0.00 0.53 0.51
0.56 0.00 0.00 0.51 0.53
0.55 0.00 0.00 0.51 0.51
0.55 0.00 0.00 0.51 0.49
0.55 0.00 0.00 0.52 0.49
0.56 0.00 0.53 0.51 0.48
My goal is finding the zero values 5-10 neighbors with coordinates and values.
Can anybody help me?
All the best
In order to find all nearest neighbors in a 5x5 box around each zero pixel we can use 2d convolution:
X1=conv2(double(~X),ones(5),'same')>0;
This yields a binary matrix with 1 in the places of ALL the nearest neighbors positions around zero pixels. finding the rows and cols for all the nearest neighbors without the zeros is just:
[row2 col2]=find(X1.*X);
Then the matrix that you want is:
MIS2=[row2 col2 X(row2, col2)];

What does `A(::2,3) = -1.0` do in Fortran?

I have a matrix A declared as real :: A(7,8) and intialised so that all entries are 0.0.
The following command does not provide any compiling errors.
A(::2,3) = -1.0
I realise that the columns affected will be only column 3.
What about the rows? Does ::2 mean rows 1 and 2? Or something else?
I printed out the matrix, but couldn't understand the pattern produced.
Here (for completeness):
do, i=1,7
write(*, "(f5.2)") ( A(i,j), j=1,8 )
enddo
0.00 i = 1
0.00
-1.00
0.00
0.00
0.00
0.00
0.00 ----
0.00 i = 2
0.00
0.00
0.00
0.00
0.00
0.00
0.00 ----
0.00 i = 3
0.00
-1.00
0.00
0.00
0.00
0.00
0.00 ----
0.00 i = 4
0.00
0.00
0.00
0.00
0.00
0.00
0.00 ----
0.00 i = 5
0.00
-1.00
0.00
0.00
0.00
0.00
0.00 ----
0.00 i = 6
0.00
0.00
0.00
0.00
0.00
0.00
0.00 ----
0.00 i = 7
0.00
-1.00
0.00
0.00
0.00
0.00
0.00
Looking at it now, it looks like it starts at i=1 and adds 2 to i until it reaches the bounds of the matrix. Is this correct?
Does this mean that ::2 is equivalent to 1:7:2 ("from 1 to 7 with a step of 2)?
Looking at the documentation, we see:
print array-expression [first-expression : last-expression : stride-expression]
where:
array-expression Expression that should evaluate to an array type.
first-expression First element in a range, also first element to be
printed. Defaults to lower bound.
last-expression Last element in a range, but might not be the last
element to be printed if stride is not equal to 1. Defaults to upper
bound.
stride-expression Length of the stride. Defaults to 1.
So if first-expression and last-expression are omitted, they default to lower bound and upper bound respectively.
Yes, that's right - it's the same as 1:7:2, as you can see from the output, it's setting the 3rd element in the sub-array to -1 for every 2nd sub array

How can I profile template performance in Template::Toolkit?

What's the best method for benchmarking the performance of my various templates when using Template::Toolkit?
I want something that will break down how much cpu/system time is spent processing each block or template file, exclusive of the time spent processing other templates within. Devel::DProf, for example, is useless for this, since it simply tells me how much time is spent in the various internal methods of the Template module.
It turns out that Googling for template::toolkit profiling yields the best result, an article from November 2005 by Randal Schwartz. I can't copy and paste any of the article here due to copyright, but suffice to say that you simply get his source and use it as a module after template, like so:
use Template;
use My::Template::Context;
And you'll get output like this to STDERR when your script runs:
-- info.html at Thu Nov 13 09:33:26 2008:
cnt clk user sys cuser csys template
1 0 0.06 0.00 0.00 0.00 actions.html
1 0 0.00 0.00 0.00 0.00 banner.html
1 0 0.00 0.00 0.00 0.00 common_javascript.html
1 0 0.01 0.00 0.00 0.00 datetime.html
1 0 0.01 0.00 0.00 0.00 diag.html
3 0 0.02 0.00 0.00 0.00 field_table
1 0 0.00 0.00 0.00 0.00 header.html
1 0 0.01 0.00 0.00 0.00 info.html
1 0 0.01 0.01 0.00 0.00 my_checklists.html
1 0 0.00 0.00 0.00 0.00 my_javascript.html
1 0 0.00 0.00 0.00 0.00 qualifier.html
52 0 0.30 0.00 0.00 0.00 referral_options
1 0 0.01 0.00 0.00 0.00 relationship_block
1 0 0.00 0.00 0.00 0.00 set_bgcolor.html
1 0 0.00 0.00 0.00 0.00 shared_javascript.html
2 0 0.00 0.00 0.00 0.00 table_block
1 0 0.03 0.00 0.00 0.00 ticket.html
1 0 0.08 0.00 0.00 0.00 ticket_actions.html
-- end
Note that blocks as well as separate files are listed.
This is, IMHO, much more useful than the CPAN module Template::Timer.