How to calculate Trailing Moving Sums going up vertically in a table? - matlab

I have the following table: Table
Columns 'L' and 'U' if the table consist of cells that contain object names that correspond to the headers in columns 4-281. Example
Goal: For every date verify what objects are in 'L' (respectively 'U') and sum the aggregate of those objects' 4-point trailing moving sum and its standard deviation (going up in the table!) and store it in a new variable, e.g. LSum and LStd for 'L' as well as USum and UStd for 'U'. For dates with insufficient values, e.g. 15-Jul-2016 with only 3 instead of 4 time steps ahead, return NaN's.
How I would start:
for row=1:size(ABC,1)
row_values = ABC{row,:};
row_values = row_values(4:end);
% How to make the loop for columns L and U where there are multiple objects in one cell?
% How can I use 'movsum' and 'movstd' here to calculate values vertically going up?
end;
Thanks a lot for your help!

Maybe you could use the functions cell2mat and cellfun to achieve your goal.
With these functions you can:
Convert your cell matrix to a double matrix in order to perform (cell2mat)
Perform a certain operation on all cell elements (cellfun)

Related

how to store data without dynamically naming variables

I have 40 variables. The 40 variables names are in a cell array (40 x 1).
Each variable will have a matrix. The matrix is of type of double and will be size 5000 x 150. It will also have a vector of size 1 x 150 & one last vector 1 x 4.
Initially I was going to dynamically name each struct with its variable name in the cell array. So would look like something like below (assuming variable name is ABC),
ABC.dataMatrix
ABC.dataVec
ABC.summaryData
All the variables would be saved to a directory.
However I've read using eval isn't a very good idea so guessing my idea isn't the best way to go about this problem. What would be the best way to go about this problem?
You can either use struct arrays with dynamic field names, as #Shai and #RobertStettler suggest.
However, another option is a table. This might be more appealing if you want to see your data in one big matrix, and you can give each table row the name of your variables too! Note that the rows in a table would then be what you call your variables, but MATLAB calls the table columns its variables.
Also note that using a table can be more difficult than using struct or cell arrays, but if you know how to use them, you can handle a table too.
An example:
% create dummy data
rowNames = {'a';'b';'c'};
M = {ones(3); zeros(3); nan(3)}; % a cell, one element per item in rowNames
V = [1 2; 3 4; 5 6]; % a matrix of vectors, each row containing a vector for every item in rowNames
% create a table
T = table(M,V,'RowNames',rowNames); % this is where your variable names could go
Now, to access data you could use (some examples):
T(2,:) or T('b',:), return a table for all data on the 'b' row.
T(:,2) or T(:,'V'), return a table of variable V for all rows.
T.V or T{:,2} or T{:,'V'} or T.(2), return matrix V for all rows. This syntax is similar to accessing a (dynamic) field name of a struct.
T{3,1} or T{'c',1} or T.M('c'), return cell M for row 'c'. This syntax is similar to accessing a cell, but with more advanced possibilities, i.e. the ability to access the table via row or variable names.
T{3,1}{:} or T{'c',1}{:} or T.M{'c'}, return cell contents M for row 'c'.
And even more complex: T('a',:).M{:} is a complex way of accessing the cell content of M for row 'a', which can be done with T{1,1}{:} or T.M{'a'} or T{'a','M'}{:} or T.M{1} as well.
In your case you would en up with a 40x3 table, with every row what you call a variable and the first column the matrices (in cell arrays), and the last two columns the vectors (as well in cell arrays or as a 40xm double, m being the length of your vector).

Matlab xlsread: force empty cells to be read as NaN

I need to import data from a square region (10 by 10 cells) on an Excel sheet into Matlab.
All data in the region are numerical, but some outer rows and columns of the region are empty.
In Matlab I still want to have a 10 by 10 matrix of doubles with NaNs in places where there are empty cells in Excel (also in outer rows and columns).
If I use xlsread then empty outer rows and columns are automatically truncated.
Needless to say that all should be done automatically without the knowledge how many empty outer rows and columns are there.
How can I do this?
Let's say your 10 by 10 spreadsheet's first row and column and last row and column are empty (like this). Using:
[num,txt,raw] = xlsread('myfile.xlsx',1,'A1:J10'); % Read input.
will return:
num 8x8 double
txt 0x0 cell
raw 10x10 cell
In num, non-scalar leading rows and columns are automatically truncated, while in txt any numerical values are omitted. However, raw contains all information, so it can be used to extract the numerical values:
raw(cellfun(#ischar,raw)) = {NaN}; % Set non-scalar values to missing.
A = cell2mat(raw); % Convert to matrix.

Finding similar rows in MATLAB

I have a matrix with a large number of rows. I have another matrix that I will loop through one row at a time. For each row in the second matrix, I need to look for similar rows in the first matrix. Once all the similar rows are found, I need to know the row numbers of the similar rows. These rows will almost never be exact, so ismember does not work.
Also, the solution would preferably (not necessarily, however) give some way to set a level of similarity that would trigger the code to say it is similar and give me the row number.
Is there any way to do this? I've looked around, and I can't find anything.
You could use cosine distance, which finds the angle between two vectors. Similar vectors (in your case, a row and your comparison vector) have a value close to 1 and dissimilar vectors have a value close to 0.
function d = cosSimilarity(u, v)
d = dot(u,v)/(norm(u)*norm(v));
end
To apply this function to each to all pairs of rows in the matrices M and V you could use nested for loops. Hardly the most elegant, but it will work:
numRowsM = size(M, 1)
numRowsV = size(V, 1)
similarThresh = .9
for m = 1:numRowsM
for v = 1:numRowsV
similarity = cosSimilarity(V(v,:), M(m, :))
% Notify about similar rows
if similarity > similarThresh
disp([num2str(m) ' is similar to a row in V'])
end
end
end
Instead of nested for loops, there are definitely other ways. You could start by looking at the solution from this question, which will help you avoid the loop by converting the rows of the matrix into cells of a cell array and then applying the function with cellfun.

Creating all possible combination of rows in matlab

I have a matrix which is 9x10000 size.
So rows are R1, R2, upto R9.
I want to generate all possible combination of the rows such as
[R1;R2] [R1;R3].. [R1;R9]
[R1;R2;R3]...[R1;R2;R4]... [R1;R2:R3;R4;..R8]
I am currently doing this using for loops.
Is there any better way of doing this.
Basically, counting up the binary from 1 to 2^9-i indicates which rows need to be selected:
M=... your matrix
S=dec2bin(1:2^size(M,1)-1)=='1';
allSubsets=cell(size(S,1),1);
for ix=1:size(S,1)
allSubsets{ix}=M(find(S(ix,:)),:);
end
As in the comment, I'm not sure if you always want the first row. This code doesn't do that, but you can modify it for that easily enough. It still uses for loops, but relies on the "nchoosek" function for the row index generation.
%generate data matrix
nMax=9; %number of rows
M=rand(nMax,1e4); %the data
%cell array of matrices with row combinations
select=cell(2^nMax-nMax-1,1); %ignore singletons, empty set
%for loop to generate the row selections
idx=0;
for i=2:nMax
%I is the matrix of row selections
I=nchoosek(1:nMax,i);
%step through the row selections and form the new matrices
for j=1:size(I,1)
idx=idx+1; %idx tracks number of entries
select{idx}=M(I(j,:),:); %select{idx} is the new matrix with selected rows
%per Floris' comment above you could do
%select{idx}=I(j,:); %save the selection for later
end
end
The function nchoosek, when given a vector, will return all possible ways to choose k values from that vector. You can trick it into giving you what you want with
allCombis = unique(nchoosek([zeros(1,9) 1:9], 9), 'rows');
This will include all possible ways to select 9 values from the set that includes nine zeros, plus the indices of each of the rows. Now you have every possible combination (including "no row at all"). With this matrix generated just once, you can find any combination easily - without having to store them all in memory. You can now pick you combination:
thisNumber = 49; % pick any combination
rows = allCombis(thisNumber, :);
rows(rows==0)=[]; % get rid of the zeros
thisCombination = myMatrix(rows, :); % pick just the rows corresponding to this combination

calculating the number of columns in a row of a cell array in matlab

i've got a cell array full of numbers, with 44 rows and different column length in each row
how could i calculate the number of columns in each row?(the columns which their contents are not empty)
i've used 2 different ways which both of them where wrong
the 1st one:
%a is the cell array
s=length(a)
it gives 44 which is the number of rows
the 2nd one
[row, columms]=size(a)
but it doesn't work either cause the number of columns is different in each row.
at least i mean the number of columns which are not empty
for example i need the number of columns in row one which it is 43(a{1 1:43}) but it gives the number of columns for each elements like a{1,1} which is 384 or a{1,2},a{1,3} and so on
You need to access each member of the cell array separately, you are looking for the size of the data contained in the cell - the cell is the container. Two methods
for loop:
cell_content_lengths=zeros(1,length(a));
for v=1:length(a)
cell_content_lengths(v)=length(a{v});
end
cellfun:
cell_content_lengths=cellfun(#length,a);
Any empty cells will just have length 0. To extend the for-loop to matrices is trivial, and you can extend the cellfun part to cells containing matrix by using something like this, if you are interested:
cell_content_sizes=cell2mat(cellfun(#length,a,'uniformoutput',false));
(Note for the above, each element of a needs to have the same dimension, otherwise it will give errors about concatenating different size matrices)
EDIT
Based on your comment I think I understand what you are looking for:
non_empty_cols = sum(~cellfun(#isempty,a),2);
With thanks to #MZimmerman6 who understood it before me.
So what you're really asking, is "How many non-empty elements are in each row of my cell array?"
filledCells = ~cellfun(#isempty,a);
columns = sum(filledCells,2);