Least mean square optimization with a constraint in Matlab - 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.

Related

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

Read a line from textfile in matlab based on two first variables

I have a big text file which gives me solar irradiance according to latitude and longitude with a 22 year monthly average.
These data are regional averages; not point data.
Created: March 12, 2008
Lat Lon Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Ann
-90 -180 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -179 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -178 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -177 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -176 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -175 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -174 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -173 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -172 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -171 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -170 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -169 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -168 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -167 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -166 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -165 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -164 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -163 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -162 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
-90 -161 9.63 5.28 0.75 0.00 0.00 0.00 0.00 0.00 0.10 3.24 8.28 10.97 3.19
Now, I want to find monthly average for lat=-90 & long=-166. How can I call them into my workspace variable?
Also, while working in matlab, will it be better to call this whole text file when we run the code or should we copy paste the whole text file in the code to make calculations faster and space efficient.
Thank you for your help.
I'm personally a fan of the table data type. I would do something like this:
t = readtable('punit.txt','HeaderLines',2,'Delimiter',' '); % read into table
index = t.Lat == -90 & t.Lon==-166; % create a binary indicator as to whether
% each row matches the criteria
my_data = t{index, 3:14}; % extract the data into a matlab array
mean_my_data = mean(my_data, 2); % calculate mean (2 makes it along columns)
You probably already know this, but I would put this code in a file like my_script.m and then call the script from the matlab workspace with my_script.
Matthew's answer is perfectly acceptable. In fact, it's the approach I'd side with. However, if you don't have access to the table interface as it was introduced in R2013b, dlmread may be more suitable for use. Simply skip three lines and use space as the delimiter to read in that matrix into MATLAB.
Once you read in this matrix, search for all rows where the first column has -90 and the second column has -166, index into your matrix and find the mean over each column. Assuming your text file is called data.txt:
data = dlmread('data.txt', ' ', 3, 0);
ind = data(:,1) == -90 & data(:,2) == -166;
extract = data(ind,3:end);
mean_data = mean(extract, 1);
The first line of code reads in the text file as a numerical matrix and skips the first three lines. Spaces are used as the delimiter. Next, we find a Boolean vector which finds all rows where the first column is -90 and simultaneously the second column being -166. Once we find these rows, we subset into the data and extract only the third column and onwards as we don't want to include the latitude and longitude as part of the calculations.
Once we extract this data, we find the averages of each month individually using mean and to find the column-wise averages, use the second parameter with the value of 1, meaning that the averages over the first dimension or the rows is what is sought.
Try Matlab Import Data button Go to Home tab-> in the Variable section-> select Import Data.
Select your text file and get whichever row or column you want to import.
Alternatively, right click name of the file in Current Folder browser and select Import Data. --> The Import Tool opens.
it is possible to create variables also in your Matlab workspace.
It works better than copy pasting manually and less time consuming.

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

Use matlab to arrange excel data

I have data in excel like this:
id date value
a 9/17/2012 0.25
a 9/18/2012 0.48
a 9/19/2012 0.29
a 9/20/2012 0.46
a 9/21/2012 0.17
a 9/24/2012 0.89
a 9/25/2012 0.20
a 9/26/2012 0.65
a 9/27/2012 0.26
b 9/17/2012 0.83
b 9/18/2012 0.87
b 9/19/2012 0.40
b 9/20/2012 0.33
b 9/21/2012 0.71
b 9/24/2012 0.13
b 9/25/2012 0.91
b 9/26/2012 0.73
b 9/27/2012 0.87
c 9/17/2012 0.47
c 9/18/2012 0.15
c 9/19/2012 0.73
c 9/20/2012 0.47
c 9/21/2012 0.03
c 9/24/2012 0.23
c 9/25/2012 0.21
c 9/26/2012 0.39
c 9/27/2012 0.77
and I would like to use Matlab to re-arrange to:
date a b c
9/17/2012 0.25 0.83 0.47
9/18/2012 0.48 0.87 0.15
9/19/2012 0.29 0.40 0.73
9/20/2012 0.46 0.33 0.47
9/21/2012 0.17 0.71 0.03
9/24/2012 0.89 0.13 0.23
9/25/2012 0.20 0.91 0.21
9/26/2012 0.65 0.73 0.39
9/27/2012 0.26 0.87 0.77
What's the easiest way to do this?
Use:
importdata or xlsread
join (statistics toolbox) or see MATLAB Combine matrices of different dimensions, filling values of corresponding indices.