I successfully created a moving window of n=85 for my data and put the windows in a cell array:
n = 37886;
cell_array=cell(n,1);
a = 1:n;
for T=1:n-84
M=a(T:T+84);
tmpstruct=struct('M',M);
cell_array{T}=tmpstruct;
end
However, I need to transpose M, which is the data of window values located within the structure within the cell array. That is, instead of the vector of window values being 1x85, I want them to be 85x1.
I've attempted transposing with this:
cell_array = cellfun(#transpose,cell_array,'UniformOutput',false);
But this code doesn't work for me.
Any feedback on how to transpose my windows will be greatly appreciated. I need to transpose 252 cell arrays, so doing this manually is out of the question.
If I clearly understood, what about:
n = 37886;
cell_array=cell(n,1);
a = 1:n;
for T=1:n-84
M=a(T:T+84);
tmpstruct=struct('M',M'); % Modified line to transpose windows!
cell_array{T}=tmpstruct;
end
If you prefer named functions:
n = 37886;
cell_array=cell(n,1);
a = 1:n;
for T=1:n-84
M=a(T:T+84);
tmpstruct=struct('M',transpose(M)); % Modified line to transpose windows!
cell_array{T}=tmpstruct;
end
Related
I have an excel file from which I obtained two string arrays, Titles of dimension 6264x1 and another Names of dimension 45696x1. I want to create an output matrix of size 6264x45696 containing in the elements a 1 or 0, a 1 if Titles contains Names.
I think I want something along the lines of:
for (j in Names)
for (k in Titles)
if (Names[j] is in Titles[k])
write to excel
end
end
end
But I don't know what functions I should use to achieve what I have in the picture. Here is what I have come up with:
[~,Title] = xlsread('exp1.xlsx',1,'A3:A6266','basic');
[~,Name] = xlsread('exp1.xlsx',2,'B3:B45698','basic');
A = cellstr(Title);
GN = cellstr(Name);
BinaryMatrix = false(45696,6264);
for i=1:1:45696
for j=1:1:6264
if (~isempty(ismember(A,GN)))
BinaryMatrix(i,j)= true;
end
end
end
the problem with this code is that it never finishes running, although there are no suggestions within matlab.
You can use third output of unique to get numbers corresponding to each string element and use bsxfun to compare numbers.
GN = cellstr(Name);
A = cellstr(Title);
B = [ GN(:); A(:)];
[~,~,u]= unique(B);
BinaryaMatrix = bsxfun(#eq, u(1:numel(GN)),u(numel(GN)+1:end).');
ismember can handle cell arrays of character vectors. Its second output tells you the information you need, from which you can build the result using sparse (it could also be done by preallocating and using [sub2ind):
[~, m] = ismember(Titles, Names);
BinaryMatrix = full(sparse(nonzeros(m), find(m), true, numel(Names), numel(Titles)));
This is my first time posting so i hope you can help me. I am trying to write a function in matlab.
I have laded data from a file into a cell array. First column contains statements and the second contains T for true og F for false. I now want to split this array into a cell array with the statements and a logical vector with 1 for True and -1 for false.
I use the fgetl within a loop to read all the lines into the cellarray
Try to write it a bit more neatly next time, and consider including a small example.
Here is what you seem to be looking for:
Suppose you have a matrix M and want to split that into M_true and M_false
M = {1,'T';
22,'F';
333,'T'}
idx_T=strcmp(M(:,2),'T')
M_true = M(idx_T,1)
M_false = M(~idx_T,1)
I need to create a 1-D array of 2-D arrays, so that a program can read each 2-D array separately.
I have a large array with 5 columns, with the second column storing 'marker' data. Depending on the marker value, I need to take the corresponding data from the remaining 4 columns and put them into a new array on its own.
I was thinking of having two for loops running, one to take the target data and write it to a cell in the 1-D array, and one to read the initial array line-by-line, looking for the markers.
I feel like this is a fairly simple issue, I'm just having trouble figuring out how to essentially cut and paste certain parts of an array and write them to a new one.
Thanks in advance.
No for loops needed, use your marker with logical indexing. For example, if your large array is A :
B=A(A(:,2)==marker,[1 3:5])
will select all rows where the marker was present, without the 2nd col. Then you can use reshape or the (:) operator to make it 1D, for example
B=B(:)
or, if you want a one-liner:
B=reshape(A(A(:,2)==marker,[1 3:5]),1,[]);
I am just answering my own question to show any potential future users the solution I came up with eventually.
%=======SPECIFY CSV INPUT FILE HERE========
MARKER_DATA=csvread('ESphnB2.csv'); % load data from csv file
%===================================
A=MARKER_DATA(:,2); % create 1D array for markers
A=A'; % make column into row
for i=1:length(A) % for every marker
if A(i) ~= 231 % if it is not 231 then
A(i)=0; % set value to zero
end
end
edgeArray = diff([0; (A(:) ~= 0); 0]); % set non-zero values to 1
ind = [find(edgeArray > 0) find(edgeArray < 0)-1]; % find indices of 1 and save to array with beginning and end
t=1; % initialize counter for trials
for j=1:size(ind,1) % for every marked index
B{t}=MARKER_DATA(ind(j,1):ind(j,2),[3:6]); % create an array with the rows from the data according to indicies
t=t+1; % create a new trial
end
gazeVectors=B'; % reorient and rename array of trials for saccade analysis
%======SPECIFY MAT OUTPUT FILE HERE===
save('Trial_Data_2.mat','gazeVectors'); % save array to mat file
%=====================================
I have a large structure array.
I would like to perform a sensitivity analysis on a function that processes this array.
So, say my structure array has name 's', 10,000 elements, and field names 'x' and 'y'.
I'd like to do the following:
xs = [s(:).x];
xs = xs + 5*randn(size(xs));
s(:).x = xs;
Sadly, the last step is not valid matlab. Any thoughts? Was hoping to avoid a loop.
From this answer and after playing around with deal. I think I have what you are looking for but it requires converting xs into a cell array using num2cell:
xs_cell = num2cell(xs); % convert matrix to cell array.
[S(:).X]=xs_cell{:}; % update the values in the field X
I want to plot several curves, each having a different length. So, I've placed each curve as an array in a cell index, Y (this allows me to index through arrays of different sizes inside a FOR loop). I use "hold all" below to enable each iteration of the FOR loop to plot each new array in the cell array Y inside the same plot.
hold all;
for i = 1:1:length(maxusers)
time = increment*(0:1:length(Y{i})-1);
plot(time,Y{i,1,:});
end
While the use of a cell array here does simplify plotting the various curves inside Y, the problem I'm having is creating legend. Currently I'm using a really long/ugly switch statement to cover every possible scenario, but I think there should be a more elegant solution.
If I have an array (where maxusers=4, for example) that is:
filesize = [10 100 200 300];
I know the legend Matlab command that works is:
legend(num2str(filesize(1)),num2str(filesize(2)),num2str(filesize(3)),num2str(filesize(4)));
but I get stuck trying to create a legend command when the number of curves is a variable given by maxusers. Any ideas? Thanks in advance.
Try this:
>> filesize = [10 100 200 300];
>> str = strtrim(cellstr(int2str(filesize.'))) %'# Create a cell array of
%# strings
str =
'10'
'100'
'200'
'300'
>> legend(str{:}); %# Pass the cell array contents to legend
%# as a comma-separated list