sum specific columns in matlab vectorized - matlab

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.

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.

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

Least mean square optimization with a constraint in Matlab

I have a piece of code in matlab:
deg = 0:57;
theta = deg*pi/180;
N = 16;
lambda = 0.1;
dxy = 0.4*lambda;
nn = 1:N;
y = (nn - 0.5*(N+1))*dxy;
BB = [0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 0.63 0.57 0.29 0.23 0.20 0.17 0.15 0.14 0.12 0.11 0.10 0.10 0.09 0.09 0.08 0.08 0.07 0.07 0.07 0.06 0.06 0.06 0.06 0.00 0.00 0.00];
AA = exp(2*pi*1j/lambda*sin(theta')*y);
w = AA \ fliplr(BB)';
I want a least mean square solution to w for AA*w = BB' such that
the absolute values of elements of w equal to 1.

Rescale signal 1 by following signal 2 matlab

Let say I have
A = [0 0 0 0 1 1 1 1 1 0 0 1 1 1 1 0 0 0 1 1 1 1 1 ]
B = [0.01 0.02 0.01 0.03 0.04 0.05 0.07 0.04 0.03 0.01 0.01 0.03 0.04 0.03 0.02 0.01 0.011 0.02 0.03 0.04 0.05 0.04 0.01]
How can I rescale A follow maximum number of B.
The result should be
C = [0 0 0 0 0.07 0.07 0.07 0.07 0.07 0 0 0.04 0.04 0.04 0.04 0 0 0 0.05 0.05 0.05 0.05 0.05]
You can use accumarray like so:
subs = cumsum([diff(A) > 0, 0]).*A + 1; %//Similar to bwlabel if you have the image processing toolbox...
maximums = accumarray(subs(:), B(:), [], #max);
maximums(1) = 0;
C = maximums(subs)
Is it what you want to do?
C = A*max(B)/max(A)

Extracting lower Triangular matrix without considering diagonal element

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