Trying to Iterate Through MultiDimensional Matrix
Show older comments
Hello all! I am trying to iterate through a multi-dimensional matrix which is in a separate .mat file in order to use mathetmatical operations on certain values which meet required parameters. What is the process of iterating through each value in this file called A to check if each individual number meets these certain requirements?
Thanks!
Answers (1)
Image Analyst
on 16 Dec 2018
If you can get the matrix from the .mat file, like
s = load(filename); % Load file into structure.
m = s.m; % Extract matrix m from the structure s.
then you can create a mask. For example to find indexes of all values of m greater than 9:
mask = m > 9; % A logical vector.
Then you can do the operation vectorized without having to iterate over it.
For example, let's say you want to take the square root of the values greater than 9. You'd do
m(mask) = sqrt(m(mask));
Take note that the mask (which is a logical vector) must show up on both sides of the operation to make sure that the values not meeting the criteria are operated on.
Categories
Find more on Author Block Masks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!