Iterating over array in pair-wise manner and calling function with two args and taking max over all pairs

조회 수: 2 (최근 30일)
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
dpb
dpb 2022년 6월 10일
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
Voss 2022년 6월 10일
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);

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2022년 6월 10일
편집: Voss 2022년 6월 10일
The problem is these lines:
region1 = regions(i);
region2 = regions(j);
Since regions is a cell array, the above lines of code make region1 and region2 cell arrays as well (in particular, they are scalar cell arrays). You should use curly braces instead of parentheses, to access the contents of the particular cell in each case:
region1 = regions{i};
region2 = regions{j};
To illustrate the difference with a concrete example:
regions = {[1 2; 3 4],[5 6; 7 8; 9 10]}
regions = 1×2 cell array
{2×2 double} {3×2 double}
regions(1) % 1-by-1 cell array containing a 2-by-2 numeric matrix
ans = 1×1 cell array
{2×2 double}
regions{1} % 2-by-2 numeric matrix
ans = 2×2
1 2 3 4
By the way, the approach you've taken here (that is, making regions a cell array of N-by-2 matrices, where N can vary from one matrix to the next) is valid and is a good way to make a "list of lists" in MATLAB (there are no "lists" per se in MATLAB).

추가 답변 (1개)

dpb
dpb 2022년 6월 10일
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...

카테고리

Help CenterFile Exchange에서 Point Cloud Processing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by