Iterating over array in pair-wise manner and calling function with two args and taking max over all pairs
Show older comments
I am using a library function written by someone else. It is here. I am calling that function with no problem:
roi_1 = [0 0 0;
1 1 1;
2 2 2;
3 2 1;
4 1 0.25;
5 1.25 1;
6 3.25 3];
roi_2 = [rand rand rand;
rand rand rand;
rand rand rand;
rand rand rand;
rand rand rand;
rand rand rand;
rand rand rand;
rand rand rand];
distance = ModHausdorffDist( roi_1, roi_2 );
distance
Now I'm trying to write code that iterates over a list-like or array-like structure with possibly more than two elements and find the max value of the ModHausdorffDist of all pairs:
function [multiRegionHausdorffDistance] = MultiRegionMHD(regions)
%MultiRegionMHD This function returns a measure of how 'spread-out' regions
%are.
% This function returns a measure of how 'spread-out' or dispered regions are. The input
% variable should be a list of lists. Each list is a region consisting
% of a list of points. In particular, it can be a list of voxels
% denoting ROIs in an MRI scan, for example.
maxDist = 0.0;
m = length(regions);
for i = 1 : m - 1
for j = i + 1 : m
region1 = regions(i);
region2 = regions(j);
dist = ModHausdorffDist(region1, region2);
if dist > maxDist
maxDist = dist;
end
end
end
multiRegionHausdorffDistance = maxDist;
end
Here is a call to that code:
n = 10;
roi_1 = zeros(n, 3);
roi_1(:,1)= randi(128, n, 1);
roi_1(:,2)= randi(128, n, 1);
roi_1(:, 3) = randi(128, n, 1);
n = 8;
roi_2 = zeros(n, 3);
roi_2(:,1)= randi(128, n, 1);
roi_2(:,2)= randi(128, n, 1);
roi_2(:, 3) = randi(128, n, 1);
dist = MultiRegionMHD({roi_1; roi_2});
fprintf('Result of first example: %f\n', dist)
I am getting this error:
>> SampleMultiRegionMHDCall
Operator '-' is not supported for operands of type 'cell'.
Error in ModHausdorffDist (line 47)
tempdist = norm(A(a,:)-B(b,:));
Error in MultiRegionMHD (line 14)
dist = ModHausdorffDist(region1, region2);
Error in SampleMultiRegionMHDCall (line 15)
dist = MultiRegionMHD({roi_1; roi_2});
I think my intent here is pretty clear. I'm unfamiliar with MATLAB arrays, matrices, and lists, although I've spent a couple of hours looking at the documentation and trying to fix this. I don't know whether it would be better to fix the MultiRegionMHD method or the call to it.
What am I doing wrong and what is the best way to fix it?
2 Comments
dpb
on 10 Jun 2022
n = 10;
roi_1 = zeros(n, 3);
roi_1(:,1)= randi(128, n, 1);
roi_1(:,2)= randi(128, n, 1);
roi_1(:, 3) = randi(128, n, 1);
n = 8;
roi_2 = zeros(n, 3);
roi_2(:,1)= randi(128, n, 1);
roi_2(:,2)= randi(128, n, 1);
roi_2(:, 3) = randi(128, n, 1);
is much more simply written as
MAXI=128; % don't bury magic constants inside code; use variables
n = 10;
c=3;
roi_1=randi(MAXI,n,c);
n = 8;
roi_2=randi(MAXI,n,c);
Voss
on 10 Jun 2022
This
roi_2 = [rand rand rand;
rand rand rand;
rand rand rand;
rand rand rand;
rand rand rand;
rand rand rand;
rand rand rand;
rand rand rand];
can be written as this
roi_2 = rand(8,3);
Accepted Answer
More Answers (1)
dpb
on 10 Jun 2022
MAXI=128; % don't bury magic constants inside code; use variables
n = 10;
c=3;
roi_1=randi(MAXI,n,c);
n = 8;
roi_2=randi(MAXI,n,c);n = 10;
You wrapped the two arrays into a cell array when you called MultiRegionMHD by using the curlies "{}"
Why did you do that?
Try
dist = MultiRegionMHD(roi_1; roi_2);
instead.
Supper's on the table, gotta run before can really look in more depth...
1 Comment
Paul Reiners
on 10 Jun 2022
Categories
Find more on Convert Image Type 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!