Notre Dame to host Indiana Statewide Cybersecurity Summit
|
On Thursday, April 18, Notre Dame will host the
Indiana Statewide Cybersecurity Summit in partnership with Indiana University, Indiana University–Purdue University Indianapolis, and Purdue University.
Cybersecurity is increasingly crucial for businesses worldwide, with threats ranging from individual hackers to nation-states. In response, Indiana universities are conducting groundbreaking research and bolstering capabilities to counter these cyber threats. The Summit aims to equip business professionals with advanced cybersecurity skills and knowledge, bringing together industry and academic experts to share insights and best practices.
|
|
|
Increasing the efficiency of MATLAB code
|
In general, code efficiency is important to reach optimal performance in programming. MATLAB was originally designed for matrix computations, hence the name, MATrix LAB
oratory. When we work with large numerical arrays, using best practices for basic matrix operations can vastly increase the efficiency of a MATLAB code. This guide will show you how to efficiently concatenate and use logical indexing on large numerical arrays.
Concatenation
Concatenating large numerical arrays can be costly in terms of execution time. Instead of [] concatenating them or using for loops, it is more efficient to use slices to assign values to the array’s elements. In the following example, we merge two 10,000x10,000 arrays using [] concatenation, slices and for loops.
|
N = 10000;
array_1 = rand(N);
array_2 = rand(N);
disp("Concatenation:");
result_concat = zeros(N,2*N);
tic
result_concat = [array_1, array_2];
toc
disp("Using slices:");
result_slice = zeros(N,2*N);
result_slice(:,1:N) = array_1;
tic
result_slice(:,(N+1):2*N) = array_2;
toc
disp("For loop:");
result_loop = zeros(N,2*N);
result_loop(:,1:N) = array_1;
tic
for ii = 1 : N
for jj = 1 : N
result_loop(ii,jj + N) = array_2(ii,jj);
end
end
toc
|
After running the MATLAB script, we can compare the runtime results for the different approaches.
| >> concatenation_example
Concatenation
Elapsed time is 0.716557 seconds.
Using slices:
Elapsed time is 0.363836 seconds.
For loop:
Elapsed time is 2.804125 seconds.
>>
|
Using slices is significantly faster than [] concatenating arrays or using for loops.
It is also worth mentioning that pre-allocating the concatenated array can result in better performance. In many cases, the size of the resulting array is unknown. A possible solution is to overestimate the size of the matrix and remove the unnecessary elements after merging the arrays.
Logical indexing
When we work with arrays, often we need to perform operations on a subset of the elements and the subset is defined by logical expressions. In the following simple example, we use indexing to change the value of every element of the matrix a to 6 if the value is less than 2.
|
>> a = [1,2;3,4]
a =
1 2
3 4
>> a(1,1) = 6
a =
6 2
3 4
>>
|
As the array only has four elements, we can simply set the (1,1) element of the matrix a to 6.
When we work with large numerical arrays, it would take a lot of time to check every element of the matrix manually. In these cases, using logical indexing is the best option.
Logical indexing means that a logical array is used for indexing another array with the same size. These logical arrays are created by applying logical expressions on an array. Only those elements will be extracted from the array that correspond to non-zero elements of the logical array.
In the following example, we create a 100x100 matrix with random numbers, and we change every element of the matrix a to 1 that is greater than 0.8. There are two approaches we can choose from. We can either loop through every element of the array and set the value of the elements to 1 if the value is greater than 0.8, or we can use logical indexing.
|
array_orig = rand(100);
disp("For loop:")
array = array_orig;
tic
% Loop through every element of the matrix and if the value of the
% element is greater than 0.8, set the value of the element to 1.
for ii = 1 : 100
for jj = 1 : 100
if(array(ii,jj) > 0.8)
array(ii,jj) = 1;
end
end
end
toc
disp("Logical indexing:")
array = array_orig;
tic
% Creating the logical array
array_logical = (array > 0.8);
% Use logical indexing
array(array_logical) = 1 ;
toc
|
After running the MATLAB script, we can compare the runtimes of the two approaches.
|
>> logical_indexing_example
For loop:
Elapsed time is 0.004137 seconds.
Logical indexing:
Elapsed time is 0.000774 seconds.
|
Using logical indexing is significantly faster than using for loops.
Summary
To reach better performance, always pre-allocate the resulting array and use slicing for concatenating large matrices. To perform operations on a subset of the elements of an array that is defined by a logical expression, always use logical indexing.
|
|
|
| Every Wednesday and Thursday in February
2:00 – 3:00 p.m.
via Zoom
|
This training is available to new users and current users interested in a refresher course on how to use CRC resources. Attendees learn the basics of accessing CRC resources and submitting jobs on the CRC clusters. This course is a co-requisite when receiving a CRC account. Learn more.
|
|
|
Top 10 Computation Users (January 2024)
|
|
| Chemical & Biomolecular Engineering
63,666 CPU hours
|
Civil & Environmental Engineering & Earth Sciences
37,271 CPU hours
|
Chemistry & Biochemistry
34,337 CPU hours
|
Economics
20,321 CPU hours
|
| Civil & Environmental Engineering & Earth Sciences
13,289 CPU hours
|
Civil & Environmental Engineering & Earth Sciences
13,085 CPU hours
|
Chemistry & Biochemistry
12,215 CPU hours
|
Chemical & Biomolecular Engineering
10,871 CPU hours
|
Psychology
10,582 CPU hours
|
|
|
Manage your preferences | Opt Out
using TrueRemove™
Got this as a forward? Sign up to receive our future emails.
View this email online.
|
940 Grace Hall University of Notre Dame | Notre Dame, IN 46556 US
|
|
|
This email was sent to .
To continue receiving our emails, add us to your address book.
|
|
|
|