How do I set a parameter based on individual values in an existing array? Should I use a for loop or something else?

조회 수: 1 (최근 30일)
I need to have 'maxDistLine' be different for different 'L' values (i.e., if L<300, then maxDistLine should be 70, or else if L is between 300 and 1000 maxDistLine should be 50, else maxDistLine should be 20. I've tried using a 'for loop', 'while', and 'if/else' statements and nothing seems to work. Please see attached section of code. Thanks in advance for any help you could offer!
Section of Code:
classdef TripletGraph2
properties
C; %triplet center points
T; %triplet endpoints
Cgraph %The undirected graph of the center points
ArcPoints %2x4xN list of arc points CenterA-EndpointA-EndpointB-CenterB
end
methods
function obj=TripletGraph2(fpep)
% maxDistLine=20;
maxLineLength=1800;
CTDist=35;
% maxDistLineDiff=7;
l2norm=@(z,varargin) vecnorm(z,2,varargin{:});
C=fpep(:,7:8); %Central points
T=reshape( fpep(:,1:6) ,[],2,3); %Triplet end-points Nx2x3
T=(T-C)./l2norm(T-C,2)*CTDist + C; %Normalize center to endpoint distances
DT=delaunayTriangulation(C); %Delaunay triangulation of centers
E=edges(DT); %Edge list
C1=C(E(:,1),:); %Center points listed for all DT edges
C2=C(E(:,2),:);
L=l2norm(C1-C2,2); %Center-to_center lengths for DT edges
if L<500
maxDistLine=21;
maxDistLineDiff=7;
elseif (500<L)&(L<1000)
maxDistLine=18;
maxDistLineDiff=7;
else
maxDistLine=15;
maxDistLineDiff=7;
end

답변 (2개)

the cyclist
the cyclist 2022년 7월 3일
My best guess here is that L is a vector, and you are expecting the if-else logic to work on the entire vector simultaneously. That's not how it works. For example
if L < 500
will only be true if all elements are less than 500. (The code won't check each element one-by-one.) This is described in the documentation for if, elseif, else.
So, you could loop over the elements, or you could vectorize explicitly, such as
maxDistLine = 21*(L<500) + 10*((L>=500)&(L<1000)) + 15*(L>1000)
  댓글 수: 1
Steven D
Steven D 2022년 7월 3일
편집: Steven D 2022년 7월 3일
Thanks for your input! However, I tried running your suggestion but was given the following error:
Unable to perform assignment because the size of the left side is 1-by-2772 and the size of the right side is 1-by-2688.
Error in TripletGraph2 (line 103)
EID2(:,:,j)=I;
Error in combined_foam_code2all (line 158)
obj=TripletGraph2(fpep);
Attached is the whole code (TripletGraph2) for all to view.

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


dpb
dpb 2022년 7월 3일
The above if...elseif...else...end construct works; there are other ways -- one way w/ MATLAB is
fnDL=@(L)interp1([500-eps(500),1000-eps(1000),realmax],L,'next','extrap');
using 1D lookup with builtin interp1. The eps() fixes up so the <= condition is met for the breakpoints.
An alternate that's a little more computationally efficient is a recast of the IF construct into a one-liner...
fnDL=@(L)21-3*(L>=500)-3*(L>=1000);
There are any number of ways to rewrite the above as well.
And, of course, the SWITCH construct could be used as well.
I'm sure others will think of many other similar implementations.
  댓글 수: 8
dpb
dpb 2022년 7월 4일
That just replaces a loop and the IF construct to produce the requested distance value in the code you posted...but your function doesn't return anything it calculates for anything else to be done with it, so it doesn't matter at all what it does until you decide what it is you want/need elsewhere and set up the return values to return the needed-elsewhere variables you're calculating.
Steven D
Steven D 2022년 7월 4일
Thanks again for your response. Where would you set up return values elsewhere--in the attached code (i.e., 'combined_foam_code2all.m')? If so, where would you put it? Is there a better way to close the polygonal cells (in the images above), then the current approach?

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by