Asked by Valentin
on 30 Jan 2013

The code below is meant to calculate the histogram in each 3D array of a 4D array. It spits out maxV as a matrix but I'm expecting it to be a vector (same size as minV). This only happens when I omit the variable 'distrib' from the list of output parameters. Why does minV come out as expected but not maxV?

The input can be a simple:

volume = rand(3,3,3,3); bins = 5; [distrib, maxV, minV] = myFunction(volume, bins)

compare with

[maxV, minV] = myFunction(volume, bins)

This is the function code:

function [distrib, maxV, minV] = myFunction(volume, bins, minV, maxV) % volume: 4th order tensor (example: a volume with >1 co-located attributes) % bins: number of bins in the histogram % minV: minimum value in the i-th tensor % maxV: maximum value in the i-th tensor clear minV maxV binEdge

n = size(volume,4); % check number of attributes

% if no minV input then find min values for each attribute if ~exist('minV') for i = 1:n mV = min(min(min(volume(:,:,:,i)))); minV(i) = mV; end end

% if no maxV input then find max values for each attribute if ~exist('maxV') for i = 1:n mV = max(max(max(volume(:,:,:,i)))); maxV(1,i) = mV end end

X = bins+1; % bins vector

distrib = zeros(X,n); for i = 1:n d = volume(:,:,:,i); % set the EDGE vector for the i-th attribute (same number of bins each time) binEdge = (minV(i)):((maxV(i)-minV(i))/bins):(maxV(i)); % calculate the histogram for each attribute h = hist(d(:),binEdge); % place each histogram vector into distrib matrix distrib(1:X,i) = h; end

*No products are associated with this question.*

Answer by Matt Kindig
on 30 Jan 2013

Edited by Matt Kindig
on 30 Jan 2013

Accepted answer

It's because in your second calling of the function

[maxV, minV] = myFunction(volume, bins)

you are setting the first output of your function ('distrib' in your function definition) to the variable 'maxV' and the second output ('maxV' in your function definition) to the variable 'minV'. You can call the first output variable whatever you want when you call the function, and it will take the value of the variable that is called 'distrib' in the function.

To illustrate, try this, for example:

volume = rand(3,3,3,3); bins = 5; [distrib, maxV, minV] = myFunction(volume, bins); [maxV, minV] = myFunction(volume, bins); isequal(distrib, maxV) %this should be true.

Remember that the variable names when you call the function and the variable names inside the function are, in general, completely separate. If you just want to output the 'maxV' and 'minV' variables (the second and third outputs of your function), call it as such:

[~, maxV, minV] = myFunction(volume, bins);

The tilde (~) tells Matlab to skip that output variable.

Opportunities for recent engineering grads.

## 0 Comments