# spmax

Below is a demonstration of the features of the spmax function

## Contents

clear; close all; clc;

## Syntax

[maxVal,maxInd]=spmax(A,B,dim,nanflag,logicRelevant,nanOut);

## Description

This function is like the max function but is designed for sparse arrays. In particular it allows one to "ignore zeros" in the determination of the maxima.

## Examples

Create example matrix

i=[2 1 1 2  2 3  3 4 4 5  5 5 6 6 7 8];
j=[1 1 2 3  4 5  6 7 8 9 10 11 12 13 13 13];
s=[-1 3 1 2 -1 1 -2 5 5 -1 0 2 3 10 11 NaN];
siz=max([i(:);j(:)]+1)*ones(1,2);
A=sparse(i,j,s,siz(1),siz(2),numel(s));
A=A+A';

full(A) % View matrix

L=sparse(i,j,1,siz(1),siz(2),numel(s));
logicRelevant=(L+L')>0;
ans =

Columns 1 through 13

6     0     0     0     0     0     0     0     0     0     0     0     0
0     0     2    -1     0     0     0     0     0     0     0     0     0
0     2     0     0     1    -2     0     0     0     0     0     0     0
0    -1     0     0     0     0     5     5     0     0     0     0     0
0     0     1     0     0     0     0     0    -1     0     2     0     0
0     0    -2     0     0     0     0     0     0     0     0     3    10
0     0     0     5     0     0     0     0     0     0     0     0    11
0     0     0     5     0     0     0     0     0     0     0     0   NaN
0     0     0     0    -1     0     0     0     0     0     0     0     0
0     0     0     0     0     0     0     0     0     0     0     0     0
0     0     0     0     2     0     0     0     0     0     0     0     0
0     0     0     0     0     3     0     0     0     0     0     0     0
0     0     0     0     0    10    11   NaN     0     0     0     0     0
0     0     0     0     0     0     0     0     0     0     0     0     0

Column 14

0
0
0
0
0
0
0
0
0
0
0
0
0
0

Compute maxima allong a certain direction (while omit nan is default)

amaxRows=spmax(A,[],1);
full(amaxRows)

amaxColumns=spmax(A,[],2);
full(amaxColumns)
ans =

Columns 1 through 13

6     2     2     5     2    10    11     5    -1     0     2     3    11

Column 14

0

ans =

6
2
2
5
2
10
11
5
-1
0
2
3
11
0

Including nans

amaxRows=spmax(A,[],1,'includenan');
full(amaxRows)

amaxColumns=spmax(A,[],2,'includenan');
full(amaxColumns)
ans =

Columns 1 through 13

6     2     2     5     2    10    11   NaN    -1     0     2     3   NaN

Column 14

0

ans =

6
2
2
5
2
10
11
NaN
-1
0
2
3
NaN
0

Computing maxima across all desired relevant entries (including "relevant/real zeros")

amaxRows=spmax(A,[],1,'omitnan',logicRelevant);
full(amaxRows)

amaxColumns=spmax(A,[],2,'omitnan',logicRelevant);
full(amaxColumns)
ans =

Columns 1 through 13

6     2     2     5     2    10    11     5    -1     0     2     3    11

Column 14

0

ans =

6
2
2
5
2
10
11
5
-1
0
2
3
11
0

Computin maxima across all desired relevant entries and output NaN where the sparse array only contains "non-relevant or non-real" zeros.

nanOut=1;

amaxRows=spmax(A,[],1,'omitnan',logicRelevant,nanOut);
full(amaxRows)

amaxColumns=spmax(A,[],2,'omitnan',logicRelevant,nanOut);
full(amaxColumns)
ans =

Columns 1 through 13

6     2     2     5     2    10    11     5    -1     0     2     3    11

Column 14

NaN

ans =

6
2
2
5
2
10
11
5
-1
0
2
3
11
NaN

GIBBON www.gibboncode.org

Kevin Mattheus Moerman, [email protected]

GIBBON footer text

GIBBON: The Geometry and Image-based Bioengineering add-On. A toolbox for image segmentation, image-based modeling, meshing, and finite element analysis.

Copyright (C) 2006-2021 Kevin Mattheus Moerman and the GIBBON contributors

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.