필터 지우기
필터 지우기

Error in optimconstr() when used with single string vs array of string

조회 수: 4 (최근 30일)
I'm working on MATLAB Version: 9.12.0.1884302 (R2022a) with Optimization Toolbox (9.3).
I have created 2 sets flight_name_set & Edges of type string
optimconstr gives the below error whenever the set is of single value but not on multiple values.
Error using optimconstr
Dimensions specifications must be a positive integer, string array, or a
cell array of character vectors.
For e.g
When Edges has 2 values Then there is no error.
flight_name_set =
1×5 string array
"Small-tt-1" "Medium-yy-2" "Medium-rr-3" "Jumbo-mm-4" "Jumbo-zz-5"
Edges =
1×2 string array
"W-Y" "S-Y"
optimconstr(Edges, flight_name_set, flight_name_set)
ans =
2×5×5 OptimizationConstraint array with properties:
IndexNames: {{1×2 cell} {1×5 cell} {1×5 cell}}
Variables: [1×1 struct] containing 0 OptimizationVariables
See constraint formulation with show.
but when Edges have 1 value there is error
flight_name_set =
1×5 string array
"Small-tt-1" "Medium-yy-2" "Medium-rr-3" "Jumbo-mm-4" "Jumbo-zz-5"
Edges =
"W-Y"
optimconstr(Edges, flight_name_set, flight_name_set)
Error using optimconstr
Dimensions specifications must be a positive integer, string array, or a cell array of character vectors.
With hit and trial I have to put a check on length and then use {Edges{:}} if there is only 1 value. Is there a way to use genralise statements?

채택된 답변

Raghunathraju
Raghunathraju 2023년 3월 31일
Hi Raviraj,
The 'optimconstr' function cannot have a single argument as string, The input arguments can be a string array, a cell array, or it can be a positive integer .If you want to use a string as an input argument then it must be an array of strings.
  댓글 수: 2
Ravi Raj Saxena
Ravi Raj Saxena 2023년 3월 31일
편집: Ravi Raj Saxena 2023년 3월 31일
But what if I have only 1 element?
Aurele Turnes
Aurele Turnes 2023년 3월 31일
When you have a single element, you should use a cell array of character vectors like this:
optimconstr(cellstr(Edges), flight_name_set, flight_name_set)
This is because optimconstr and optimexpr go hand-in-hand with optimvar, which accepts string inputs and name-value arguments. For this reason, when writing:
optimvar("x", "Typo", "integer")
It is impossible to know if this is a typo and means:
optimvar("x", "Type", "integer")
or if this means:
optimvar("x", {'Typo'}, {'integer'})
To avoid silently returning the wrong answer, we error and require to pass a cell array for a single string input. Optimexpr and optimconstr behave similarly for consistency.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Problem-Based Optimization and Equations에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by