Variably name histogram in for loop - matlab

I'm trying to have a hist function in a for loop because I work with varying amounts of datasets each time and its much faster and easier that having to edit a script each time, but I can't get it right. Can I have some help please? In essence I'm trying to have this in a for loop for variable number of unc{i} datasets and i number of [h{i},x{i}] resulting arrays:
[h1,x1] = hist(unc1,range);
[h2,x2] = hist(unc2,range);
[h3,x3] = hist(unc3,range);
[h4,x4] = hist(unc4,range);
Any help would be greatly appreciated. Thanking you in advance

Desclaimer: the use of eval is dangerous!
Let's say you have n uncs arrays. You can use struct to store them
for ii=1:n
cmd = sprintf( 's.unc%d = unc%d;', ii, ii );
eval( cmd );
end
Once you have the uncs is a sttruct, you can simply
for ii=n:-1:1
[h{ii} x{ii}] = hist( s.(sprintf('unc%d',ii)), range );
end
Notes:
1. Note that I used a backward loop for computing the histograms: this is a nice trick to preallocate h and x, see this thread.
2. It is extremly unwise to use eval, therefore, it might be wiser to create the different uncs arrays as a struct fields to begin with, skipping the first part of this answer.

You can put each of your input datasets in a cell array, and the output of the histograms in a second cell array.
For example,
unc1 = rand(5,1);
unc2 = rand(5,1);
unc3 = rand(5,1);
unc_cell = {unc1, unc2, unc3};
h_cell = cell(3, 1);
x_cell = cell(3, 1);
for ii = 1:3
[h{ii} x{ii}] = hist(unc_cell{ii});
end
This does require preloading all of the datasets and holding them in memory simultaneously. If this would use too much memory, you can load the datasets in the for loop rather than preloading them.
For example,
h_cell = cell(3, 1);
x_cell = cell(3, 1);
for ii = 1:3
unc = load(sprintf('data_%d.mat', ii)); %You would replace this with your file name
[h{ii} x{ii}] = hist(unc);
end

Related

loop and concatenate variable in workspace

I am trying to concatenate matrices station_1, station_2,.....station_10 from my matlab workspace and trying to concatenate all stations automatically using a loop and not calling them one by one like this
cat(1,station_1,station_2,station_3,station_4... ,station_5,station_6,station_7,station_8... ,station_9,station_10 )
Any ideas?
the code below is what i was trying to improve
for jj= 1 : 10 T= cat(1,eval(['station_', num2str(jj)])); MegaMat = cat(1,T) end
Reading your code I think at the end of your loop you will have T = station_10.
If you want to concatenate all of them you would do
T = []
for jj= 1:10
T = cat(1, T, eval(['station_', num2str(jj)]));
end
MegaMat = T;
Using eval is not a good practice. Instead of creating station_1 to station_10 you could create a cell array
station{1} = ...
station{2} = ...
Then you could iterate like
T = []
for jj = 1:length(station)
T = cat(1, T, station{jj});
end
If the number of arrays is big this will be slow due to memory reallocation and copy. In that case is more efficient to initialize T as a matrix of the final dimension and write slices.
Appendix:
There is an interesting notation trick pointed by #Cris Luengo in the comments, that is when you have a cell array station, use the notation [station{:}], I have to admit, this notation is new to me. The only caveat is that if you set the items station{i} = ... then you will have the matrices concatenated horizontally rather than vertically.
The answer from #Mateo V, is also good, probably with leas overhead since it calls eval only once. That approach can be refined giving a one linear solution, and to be honest It felt not very unreadable.
MegaMat = eval(['cat(1', num2str(1:10, ', station_%d'), ')']);
No need for loops:
str = num2str(1:10, 'station_%i,'); % returns string 'station_1, station_2, ..., station_10,'
str = str(1:end-1); % remove last comma
eval(['MegaMat = cat(1, ', str, ');'])

Sum of Data(end) in a cell array of timeseries

Given the code below:
% Generate some random data
n = 10;
A = cell(n, 1);
for i=1:n
A{i} = timeseries;
A{i}.Data = rand(100, 1);
A{i}.Time = 1:100;
end
I would like to make the sum of Data(end) without explicitly writing a for loop. Is there a smart way to select Data(end) in all cells in a single line? A{:}.Data(end) does not work.
You can do it with cellfun but that is essentially just a for loop wrapped up:
cellfun(#(x) x.Data(end), A)
I prefer Dan's answer, but for reference, I'll post an alternative using arrayfun. This is also just a for loop wrapped up to save keystrokes, but not necessarily time.
sum(arrayfun(#(n) A{n}.Data(end), 1:numel(A)))
You can also extract all of the Data fields into a single matrix, which might be worth it if you're planning on doing multiple operations on it:
A2 = [A{:}];
A3 = [A2.Data];
sum(A3(end,:))

Create a matrix combining many variables by using their names and a for loop

Suppose I have n .mat files and each are named as follows: a1, a2, ..., an
And within each of these mat files there is a variable called: var (nxn matrix)
I would like to create a matrix: A = [a1.var a2.var, ..., an.var] without writing it all out because there are many .mat files
A for-loop comes to mind, something like this:
A = []
for i = 1:n
[B] = ['a',num2str(i),'.mat',var];
A = [A B]
end
but this doesn't seem to work or even for the most simple case where I have variables that aren't stored as a(i) but rather 'a1', 'a2' etc.
Thank you very much!
load and concatenate 'var' from each of 'a(#).mat':
n = 10;
for i = n:-1:1 % 1
file_i = sprintf('a%d.mat', i); % 2
t = load(file_i, 'var');
varsCell{i} = t.var; % 3
end
A = [varsCell{:}]; % concatenate each 'var' in one step.
Here are some comment on the above code. All the memory-related stuff isn't very important here, but it's good to keep in mind during larger projects.
1)
In MATLAB, it is rarely a good idea or necessary to grow variables during a for loop. Each time an element is added, MATLAB must find and allocate a new block of RAM. This can really slow things down, especially for long loops or large variables. When possible, pre-allocate your variables (A = zeros(n,n*n)). Alternatively, it sometimes works to count backwards in the loop. MATLAB pre-allocates the whole array, since you're effectively telling it the final size.
2)
Equivalent to file_i = ['a',num2str(i),'.mat'] in this case, sprintf can be clearer and more powerful.
3)
Store each 'var' in a cell array. This is a balance between allocating all the needed memory and the complication of indexing into the correct places of a preallocated array. Internally, the cell array is a list of pointers to the location of each loaded 'var' matrix.
to create a test set...
generate 'n' matrices of n*n random doubles
save each as 'a(#).mat' in current directory
for i = 1:n
var = rand(n);
save(sprintf('a%d.mat',i), 'var');
end
Code
%%// The final result, A would have size nX(nXn)
A = zeros(n,n*n); %%// Pre-allocation for better performance
for k =1:n
load(strcat('a',num2str(k),'.mat'))
A(1:n,(k-1)*n+1:(k-1)*n+n) = var;
end

MATLAB memory allocation: loop over mat-files

I have n mat-files containing parts of a single huge matrix. I want to load the mat-files and concatenate the row,column,value-vectors to build matrix W. Right now, I'm doing the following, but it's really slow and I know I shouldn't dynamically increase the size of rows,cols,vals:
rows=[];cols=[];vals=[];
for ii=1:n
var = load( sprintf('w%d.mat', ii) );
[r,c,v] = find( var.w );
rows = [rows; r];
cols = [cols; c];
vals = [vals; v];
end
W = sparse( rows, cols, vals );
Do you know a better way? Thanks in advance!
Solution
Based on Daniel R's suggestion, I solved it like this:
rows=zeros(maxSize,1);
cols=zeros(maxSize,1);
vals=zeros(maxSize,1);
idx = 1;
for ii=1:n
var = load( sprintf('w%d.mat', ii) );
[r,c,v] = find( var.w );
len = size(r,1);
rows(idx:(idx-1+len))=r;
cols(idx:(idx-1+len))=c;
vals(idx:(idx-1+len))=v;
idx = idx+len;
end
W = sparse( rows, cols, vals );
Extremely fast, thanks a lot!
You need to preallocate an array. Assuming r,c and v have the size a*b each.
In total, you need a*n rows and b columns, thus preallocate using rows=nan(a*n,b)
To write the Data into the array, you have to set the correct indices: rows((ii-1)*a+1:ii*a,1:end)=r
A few ideas.
It looks to me the matrices have the same sizes.
Another bottleneck might be the find function.
could it happen that two matrices have different values at the same index? That might lead to problems.
You could do:
var = load( 'w1.mat' );
W = var.w;
for ii = 2:n
var = load( sprintf('w%d.mat', ii) );
ig = var.w~=0;
W(ig) = var.w(ig);
end
in case var.w is not sparse add a W = sparse(W)
Here is a trick that sometimes helps to speed things up as if your variables were initialized. However, sometimes it just does not help so the only way to find out is to try:
Replace lines like:
rows = [rows; r];
by lines like:
rows(end+(1:numel(r))) = r;

Matlab: how to implement a dynamic vector

I am refering to an example like this
I have a function to analize the elements of a vector, 'input'. If these elements have a special property I store their values in a vector, 'output'.
The problem is that at the begging I don´t know the number of elements it will need to store in 'output'so I don´t know its size.
I have a loop, inside I go around the vector, 'input' through an index. When I consider special some element of this vector capture the values of 'input' and It be stored in a vector 'ouput' through a sentence like this:
For i=1:N %Where N denotes the number of elements of 'input'
...
output(j) = input(i);
...
end
The problem is that I get an Error if I don´t previously "declare" 'output'. I don´t like to "declare" 'output' before reach the loop as output = input, because it store values from input in which I am not interested and I should think some way to remove all values I stored it that don´t are relevant to me.
Does anyone illuminate me about this issue?
Thank you.
How complicated is the logic in the for loop?
If it's simple, something like this would work:
output = input ( logic==true )
Alternatively, if the logic is complicated and you're dealing with big vectors, I would preallocate a vector that stores whether to save an element or not. Here is some example code:
N = length(input); %Where N denotes the number of elements of 'input'
saveInput = zeros(1,N); % create a vector of 0s
for i=1:N
...
if (input meets criteria)
saveInput(i) = 1;
end
end
output = input( saveInput==1 ); %only save elements worth saving
The trivial solution is:
% if input(i) meets your conditions
output = [output; input(i)]
Though I don't know if this has good performance or not
If N is not too big so that it would cause you memory problems, you can pre-assign output to a vector of the same size as input, and remove all useless elements at the end of the loop.
output = NaN(N,1);
for i=1:N
...
output(i) = input(i);
...
end
output(isnan(output)) = [];
There are two alternatives
If output would be too big if it was assigned the size of N, or if you didn't know the upper limit of the size of output, you can do the following
lengthOutput = 100;
output = NaN(lengthOutput,1);
counter = 1;
for i=1:N
...
output(counter) = input(i);
counter = counter + 1;
if counter > lengthOutput
%# append output if necessary by doubling its size
output = [output;NaN(lengthOutput,1)];
lengthOutput = length(output);
end
end
%# remove unused entries
output(counter:end) = [];
Finally, if N is small, it is perfectly fine to call
output = [];
for i=1:N
...
output = [output;input(i)];
...
end
Note that performance degrades dramatically if N becomes large (say >1000).