Matlab has a reputation for running slowly. Here are some pointers on how to speed computations, to an often unexpected degree. Subjects currently covered:
Matrix Coding
Implicit Multithreading on a Multicore Machine
Sparse Matrices
Sub-Block Computation to Avoid Memory Overflow
Matrix Coding - 1
Matlab documentation notes that efficient computation depends on using the matrix facilities, and that mathematically identical algorithms can have very different runtimes, but they are a bit coy about just what these differences are. A simple but telling example:
The following is the core of the GD-CLS algorithm of Berry et.al., copied from fig. 1 of Shahnaz et.al, 2006, "Document clustering using nonnegative matrix factorization':
for jj = 1:maxiter
A = W'*W + lambda*eye(k);
for ii = 1:n
b = W'*V(:,ii);
H(:,ii) = A \ b;
end
H = H .* (H>0);
W = W .* (V*H') ./ (W*(H*H') + 1e-9);
end
Replacing the columwise update of H with a matrix update gives:
for jj = 1:maxiter
A = W'*W + lambda*eye(k);
B = W'*V;
H = A \ B;
H = H .* (H>0);
W = W .* (V*H') ./ (W*(H*H') + 1e-9);
end
These were tested on an 8049 x 8660 sparse matrix bag of words V (.0083 non-zeros), with W of size 8049 x 50, H 50 x 8660, maxiter = 50, lambda = 0.1, and identical initial W. They were run consecutivly, multithreaded on an 8-processor Sun server, starting at ~7:30PM. Tic-toc timing was recorded.
Runtimes were respectivly 6586.2 and 70.5 seconds, a 93:1 difference. The maximum absolute pairwise difference between W matrix values was 6.6e-14.
Similar speedups have been consistantly observed in other cases. In one algorithm, combining matrix operations with efficient use of the sparse matrix facilities gave a 3600:1 speedup.
For speed alone, C-style iterative programming should be avoided wherever possible. In addition, when a couple lines of matrix code can substitute for an entire C-style function, program clarity is much improved.
Matrix Coding - 2
Applied to integration, the speed gains are not so great, largely due to the time taken to set up the and deal with the boundaries. The anyomous function setup time is neglegable. I demonstrate on a simple uniform step linearly interpolated 1-D integration of cos() from 0 to pi, which should yield zero: tic; step = .00001; fun = @cos; start = 0; endit = pi; enda = floor((endit - start)/step)*step + start; delta = (endit - enda)/step; intF = fun(start)/2; intF = intF + fun(endit)*delta/2; intF = intF + fun(enda)*(delta+1)/2; for ii = start+step:step:enda-step intF = intF + fun(ii); end intF = intF*step toc;
intF = -2.910164109692914e-14 Elapsed time is 4.091038 seconds.
Replacing the inner summation loop with the matrix equivalent speeds things up a bit:
tic;
step = .00001;
fun = @cos;
start = 0;
endit = pi;
enda = floor((endit - start)/step)*step + start;
delta = (endit - enda)/step;
intF = fun(start)/2;
intF = intF + fun(endit)*delta/2;
intF = intF + fun(enda)*(delta+1)/2;
intF = intF + sum(fun(start+step:step:enda-step));
intF = intF*step
toc;
intF = -2.868419946011613e-14 Elapsed time is 0.141564 seconds.
The core computation take
https://www.gnu.org/licenses/gpl.htmlhttps://www.gnu.org/licenses/gpl.html
Physiological waveforms - such as electrocardiograms (ECG), electroencephalograms (EEG), electromyograms (EMG) - are generated during the course of routine care. These signals contain information that can be used to understand underlying conditions of health. Effective processing and analysis of physiological data requires specialized software. The WaveForm DataBase (WFDB) Toolbox for MATLAB and Octave is a collection of over 30 functions and utilities that integrate PhysioNet's open-source applications and databases with the high-precision numerical computational and graphics environment of MATLAB and Octave.
This data set consists of Conductivity, Temperature, Depth (CTD) data in MATLAB Format from the 2002 Polar Star Mooring Cruise (AWS-02-I). These data are provided in a single mat-file (MATLAB) for the entire cruise.
Attribution 3.0 (CC BY 3.0)https://creativecommons.org/licenses/by/3.0/
License information was derived automatically
These Matlab/Simulink Files were used to generate the plots for the submitted paper „Mathematical Modeling and Simulation of Thyroid Homeostasis: Implications for the Allan-Herndon-Dudley-Syndrome“.
To reproduce the plots, please execute the files in the following order: 1: MM_Parameters_Healthy.m 2: MM_Healthy.slx 3: MM_Parameters_ADHS.m 4: MM_AHDS.slx 5: MM_Plot_Results and analogously regarding the linear case.
Attribution 4.0 (CC BY 4.0)https://creativecommons.org/licenses/by/4.0/
License information was derived automatically
Scripts and data acquired at the Mirror Lake Research Site, cited by the article submitted to Water Resources Research: Distributed Acoustic Sensing (DAS) as a Distributed Hydraulic Sensor in Fractured Bedrock M. W. Becker(1), T. I. Coleman(2), and C. C. Ciervo(1) 1 California State University, Long Beach, Geology Department, 1250 Bellflower Boulevard, Long Beach, California, 90840, USA. 2 Silixa LLC, 3102 W Broadway St, Suite A, Missoula MT 59808, USA. Corresponding author: Matthew W. Becker (matt.becker@csulb.edu).
Attribution 4.0 (CC BY 4.0)https://creativecommons.org/licenses/by/4.0/
License information was derived automatically
Matlab code and raw data
There are many MATLAB users in the Hydrology space. These scientists work as researchers and educators in academia and in agencies and institutes. Many of these institutions partner with CUAHSI and use their resources to share data and research. For data analysis and visualization, HydroShare provides integrations with Jupyter notebooks and other tools, via an ‘open with’ affordance.
MATLAB Online provides access to MATLAB from any standard web browser wherever you have internet access. It is ideal for teaching, learning and convenient, lightweight access. With MATLAB Online, you can share your scripts, live scripts, and other MATLAB files with others directly. Additionally, you can publish your scripts and live scripts to the web as PDFs or HTML and share the URL with anyone.
This web application enables the interactive exploration of MATLAB artifacts (such as Live Scripts) through a similar ‘open with’ affordance. When working with Live Scripts, users are presented with the option to open these artifacts in the Live Editor environment.
Attribution-NonCommercial-NoDerivs 4.0 (CC BY-NC-ND 4.0)https://creativecommons.org/licenses/by-nc-nd/4.0/
License information was derived automatically
Dataset and Matlab code to accompany the following manuscript: Wilson, L. Constantine, R. Pine, M.K., Farcas, A. Radford, C.A. 2022. Small boat sound diminishes the listening spaces of fishes and crustaceans.
Please note that the functions find_closest4_fast.m, linterp.m, linterp2d.m, and extract_rec_value_update.m were provided by Charlotte Findlay and Adrian Farcas. Charlotte Findlay can be contacted at charlotte_findlay@hotmail.co.uk or charlotte.findlay@bio.au.dk.
The following required functions are available from the Matlab file exchange: Jonathan Sullivan (2023). Automatic Map Scale Generation (https://www.mathworks.com/matlabcentral/fileexchange/33545-automatic-map-scale-generation), MATLAB Central File Exchange. Retrieved April 28, 2023.
Rafael Palacios (2023). deg2utm (https://www.mathworks.com/matlabcentral/fileexchange/10915-deg2utm), MATLAB Central File Exchange. Retrieved April 28, 2023.
Rafael Palacios (2023). utm2deg (https://www.mathworks.com/matlabcentral/fileexchange/10914-utm2deg), MATLAB Central File Exchange. Retrieved April 28, 2023.
The function PG_DFT.m accompanies the folllowing manuscript: Merchant, N. D., Fristrup, K. M., Johnson, M. P., Tyack, P. L., Witt, M. J., Blondel, P., & Parks, S. E. (2015). Measuring acoustic habitats. Methods in Ecology and Evolution, 6, 257–265. https://doi.org/10.1111/2041-210X.12330
CC0 1.0 Universal Public Domain Dedicationhttps://creativecommons.org/publicdomain/zero/1.0/
License information was derived automatically
Component 1 of sofware related to the paper: Teza, G., Pesci, A., Meschis, M., 2023. A MATLAB toolbox for computation of velocity and strain rate field from GNSS coordinate time series. Annals of Geophysics, Revision submitted.
Matlab code to simulate equilibrium geometry of selected cross-sections on the Lower American and Sacramento Rivers in California.
This dataset was developed as a means of identifying particular events during the SHEBA drift, and assembling in one place data necessary for driving and verifying ice ocean models. It does not include cloud or precipitation data. It does include data of the following types: meteorological, ice, sheba_gpsdata, turb_mast, profiler, ADP and bathymetry. Please see the Readme for more information.
This data set consists of Bottle Data in MATLAB Format from the Spring 2002U.S. Coast Guard Cutter (USCGC) Healy Cruise (HLY-02-01). These data are provided in a single mat-file (MATLAB) for the entire cruise.
Attribution 4.0 (CC BY 4.0)https://creativecommons.org/licenses/by/4.0/
License information was derived automatically
Instructions to read h5ad file in Matlab: A mat file of the complete lemur cell atlas dataset converted from the h5ad file is provided in the Figshare files. We also provide a Matlab script to import the h5ad file to mat file: please download the h5ad file of interest, Matlab script “LCA_h5ad2Mat.m” and Matlab function “read_csmatrix.m” to the same folder, and run “LCA_h5ad2Mat.m”. The mat file contains a single variable named “rawData”, a Matlab structure variable with the following fields:cells: a table of the sequenced cells with metadata for individual sequenced cells (features of the table includes above “/obs” and “/obsm” list for the h5ad file, e.g., cell_name, tissue, free_annotation_v1, and X_umap, but not the MHC counts which is included in tabMHC, see below).genes: gene tablename: NCBI gene symbol.highly_variable: whether the gene is highly variable (calculated for the entire dataset).mat_raw: a sparse matrix of the cell by gene transcript count (raw count).mat_X: a sparse matrix of the cell by gene transcript level after library size normalization and natural log transformation (i.e., smartseq2, ln(reads/N *1e4 +1); 10x, ln(UMI/N *1e4 +1), where N denotes the total number of reads or UMI of the cell).tabMHC: a table of the calculated raw counts for the major histocompatibility complex (MHC) genes (see the Tabula Microcebus manuscript for detail). Note the count is only available for cells sequenced by 10x method and count is NAN for cells sequenced by smartseq2 method. Both raw counts and normalized counts (labeled with prefix letter ‘n’) are provided.MHC_C_I, MHC_NC_I, MHC_all_II: sum of counts from classical Class I genes.nMHC_C_I, nMHC_NC_I, nMHC_all_II: sum of normalized counts from classical Class I genes.counts and normalized counts from individual classical Class I genes (Mimu_168, Mimu_W03, Mimu_W04, Mimu_249, nMimu_168, nMimu_W03, nMimu_W04, nMimu_249), non-classical Class I genes (Mimu_180ps, Mimu_191, Mimu_202, Mimu_208, Mimu_218, Mimu_229ps, Mimu_239ps, nMimu_180ps, nMimu_191, nMimu_202, nMimu_208, nMimu_218, nMimu_229ps, nMimu_239ps), and Class II genes (Mimu_DMA, Mimu_DMB, Mimu_DPA, Mimu_DPB, Mimu_DQA, Mimu_DQB, Mimu_DRA, Mimu_DRB, nMimu_DMA, nMimu_DMB, nMimu_DPA, nMimu_DPB, nMimu_DQA, nMimu_DQB, nMimu_DRA, nMimu_DRB). version: version of the data (name of the h5ad file converted from).
Attribution 4.0 (CC BY 4.0)https://creativecommons.org/licenses/by/4.0/
License information was derived automatically
In this appendix
This is the data and Matlab code associated with "Predicting Monoclonal Antibody Binding Sequences from a Sparse Sampling of All Possible Sequences ". Note that there are Arizona State University Patents associated with the algorithms involved in this work.
This data set consists of Bottle Data in MATLAB Format from the 2002 Polar Star Mooring Cruise (AWS-02-I). These data are provided in a single mat-file (MATLAB) for the entire cruise.
https://www.gnu.org/licenses/gpl-3.0.htmlhttps://www.gnu.org/licenses/gpl-3.0.html
The MATLAB-files contained within this dataset describe a nonlinear steady-state cornering model for a six-wheel city bus. Due to the employed multi-body approach, the wheel-configuration of the model can be changed easily. The wheel location and orientation is completely parameterized, allowing for the analysis of different types of vehicles. Emphasis is placed on the calculation of the cornering resistance power and power lost due to scrub losses to research the effect of these tire-effect on the vehicle energy consumption.
The current model includes six wheels, where the two sets of double rear
wheels have individual rotational velocities. In the model derivation,
linearizations are avoided: large angles are allowed and the non-linear Magic
Formula is employed to calculate the tire forces. Additionally, lateral load
transfer effects, due to the elevated center of gravity (CoG), are included.
The developed non-linear model has four degrees of freedom. Steady-state
solutions of the model are determined iteratively using an adapted Newton
scheme. The model enables the calculation of all tire velocities and tire
forces for a given cornering situation characterized by the cornering radius
rho and the vehicle velocity v.
This project has received funding from the European Unions Horizon 2020
research and innovation programme under grant agreement No. 713771
(EVERLASTING).
Attribution 4.0 (CC BY 4.0)https://creativecommons.org/licenses/by/4.0/
License information was derived automatically
(ii) packet error probabilities and (iii) arrival rates.
Attribution 4.0 (CC BY 4.0)https://creativecommons.org/licenses/by/4.0/
License information was derived automatically
The uploaded .ZIP file contains the MATLAB codes used in Examples 1 and 2 of the following paper
Attribution 4.0 (CC BY 4.0)https://creativecommons.org/licenses/by/4.0/
License information was derived automatically
BPM-Matlab is an open-source optical propagation simulation tool we developed in MATLAB environment for computationally efficient simulation of electric field propagation through a wide variety of optical fiber geometries using Douglas-Gunn Alternating Direction Implicit finite difference method. The validations of BPM-Matlab numerical results are provided in the article by comparing them against published data and results from state-of-the-art commercial software. The simulation tool is gratis, open-source, fast, user-friendly, and supports optional CUDA acceleration. It can be downloaded from https://gitlab.gbar.dtu.dk/biophotonics/BPM-Matlab. The software is published under the terms of the GPLv3 License.
The data available here in DTU Data can be used to reproduce the figures 1-5 in the Optics Express manuscript titled 'BPM-Matlab - An open-source optical propagation simulation tool in MATLAB'. These data are generated using BPM-Matlab software except for Data_Fig1_d.mat. We suggest the user to use Matlab 2018a or newer to open and read the data. The data set is published under the terms of the Creative Commons Attribution 4.0 License.Data_Fig1_a_b_c.matThis file can be used to reproduce Fig. 1 (a-c) of the article where BPM-Matlab is used to simulate beam propagation through a multimode fiber. The x and y axes values are available in the variables P.x and P.y. The E-field intensity at the proximal end in Fig. 1(a) can be calculated as abs(P.Einitial.').^2. The corresponding phase in Fig. 1(b) is available as angle(P.Einitial.'). The E-field intensity at the multimode fiber distal end in Fig. 1(c) can be calculated as abs(P.E.field.').^2.Data_Fig1_d.matThe corresponding BeamLab simulation results of the same multimode fiber are available in this data file. This data file is generated using BeamLab software. Use the variables bpmData.SlicesXZ.XData, bpmData.SlicesYZ.YData, and abs(bpmData.OutputField.E.x.').^2 to obtain x, y, and distal E-field intensity respectively.Data_Fig_2.matThe data from this file will generate intensity profiles of the five lowest order fiber modes supported by a straight and a bent multimode fiber corresponding to Figure 2 of the article. The variables P_noBend and P_bend are struct variables that hold information about the spatial dimensions as well as E-field profiles of the straight and bent modes. For the straight fiber case, the mode field profile is stored in P_noBend.modes(modeNumber).field, where 1x = dx*(-(Nx-1)/2:(Nx-1)/2) and y = dy*(-(Ny-1)/2:(Ny-1)/2), where Nx = size(P_noBend.modes(modeNumber).field,1), Ny = size(P_noBend.modes(modeNumber).field,2), dx = P_noBend.modes(modeNumber).Lx/Nx, and dy = P_noBend.modes(modeNumber).Ly/Ny. In a similar manner, the mode field profiles of bent multimode fiber may also be accessed from P_bend. Data_Fig3_a.matUse this data file to reproduce Figure 3(a) from the article, where numerical simulation results of different LP modes' normalized fractional power in a bent multimode fiber excited with LP01 mode are presented. The matlab command semilogy(P.z.*1e3,P.modeOverlaps,'linewidth',2)will plot the mode overlap of LP01 to all 30 guided modes in logarithmic scale. The following command legend(P.modes.label,'location','eastoutside','FontSize',6)could be used to label the modes. Set the y-limits of the plot using ylim([1e-4 2]) to visualize the contribution from only the six most excited modes. Data_Fig3_b.matLoad this data file and follow similar steps described above for Data_Fig3_a case in order to plot normalized fractional power in a bent multimode fiber excited with LP03 mode, as in Figure 3(b). Data_Fig_4.matTo reproduce Figure 4(a) from the article, use the commands imagesc(P.z,P.x,abs(P.xzSlice).^2);ylim([-1 1]*0.75e-5); to plot the intensity profile in the xz plane of a multimode fiber tapered down to be a single-mode fiber. For Figure 4(b), use plot(P.z,P.powers) that will plot the power within the simulation window against the length P.z of the fiber. Data_Fig5_a.matThis data file could be used to plot the intensity profile of the E-field at a distance of z = 5 mm after the non-twisted, straight multicore fiber distal end as given in Figure 5(a) in the article. The E-field data after propagation from the distal end is available as E_out_fft.field and the corresponding spatial dimensions are available as E_out_fft.x and E_out_fft.y. Use imagesc(x.*1e3,y.*1e3,E_abs(E_out_fft.field.').^2); axis image; to plot the field intensity profile. Similar to the above case, use the below .mat files to reproduce Figure 5 (b-d). Data_Fig5_b.mat - Twisted straight multicore fiberData_Fig5_c.mat - Non-twisted bent multicore fiberData_Fig5_d.mat - Twisted bent multicore fiber.
Matlab has a reputation for running slowly. Here are some pointers on how to speed computations, to an often unexpected degree. Subjects currently covered:
Matrix Coding
Implicit Multithreading on a Multicore Machine
Sparse Matrices
Sub-Block Computation to Avoid Memory Overflow
Matrix Coding - 1
Matlab documentation notes that efficient computation depends on using the matrix facilities, and that mathematically identical algorithms can have very different runtimes, but they are a bit coy about just what these differences are. A simple but telling example:
The following is the core of the GD-CLS algorithm of Berry et.al., copied from fig. 1 of Shahnaz et.al, 2006, "Document clustering using nonnegative matrix factorization':
for jj = 1:maxiter
A = W'*W + lambda*eye(k);
for ii = 1:n
b = W'*V(:,ii);
H(:,ii) = A \ b;
end
H = H .* (H>0);
W = W .* (V*H') ./ (W*(H*H') + 1e-9);
end
Replacing the columwise update of H with a matrix update gives:
for jj = 1:maxiter
A = W'*W + lambda*eye(k);
B = W'*V;
H = A \ B;
H = H .* (H>0);
W = W .* (V*H') ./ (W*(H*H') + 1e-9);
end
These were tested on an 8049 x 8660 sparse matrix bag of words V (.0083 non-zeros), with W of size 8049 x 50, H 50 x 8660, maxiter = 50, lambda = 0.1, and identical initial W. They were run consecutivly, multithreaded on an 8-processor Sun server, starting at ~7:30PM. Tic-toc timing was recorded.
Runtimes were respectivly 6586.2 and 70.5 seconds, a 93:1 difference. The maximum absolute pairwise difference between W matrix values was 6.6e-14.
Similar speedups have been consistantly observed in other cases. In one algorithm, combining matrix operations with efficient use of the sparse matrix facilities gave a 3600:1 speedup.
For speed alone, C-style iterative programming should be avoided wherever possible. In addition, when a couple lines of matrix code can substitute for an entire C-style function, program clarity is much improved.
Matrix Coding - 2
Applied to integration, the speed gains are not so great, largely due to the time taken to set up the and deal with the boundaries. The anyomous function setup time is neglegable. I demonstrate on a simple uniform step linearly interpolated 1-D integration of cos() from 0 to pi, which should yield zero: tic; step = .00001; fun = @cos; start = 0; endit = pi; enda = floor((endit - start)/step)*step + start; delta = (endit - enda)/step; intF = fun(start)/2; intF = intF + fun(endit)*delta/2; intF = intF + fun(enda)*(delta+1)/2; for ii = start+step:step:enda-step intF = intF + fun(ii); end intF = intF*step toc;
intF = -2.910164109692914e-14 Elapsed time is 4.091038 seconds.
Replacing the inner summation loop with the matrix equivalent speeds things up a bit:
tic;
step = .00001;
fun = @cos;
start = 0;
endit = pi;
enda = floor((endit - start)/step)*step + start;
delta = (endit - enda)/step;
intF = fun(start)/2;
intF = intF + fun(endit)*delta/2;
intF = intF + fun(enda)*(delta+1)/2;
intF = intF + sum(fun(start+step:step:enda-step));
intF = intF*step
toc;
intF = -2.868419946011613e-14 Elapsed time is 0.141564 seconds.
The core computation take