Error with find Function in MATLAB: "Incorrect number or types of i... (2024)

103 views (last 30 days)

Show older comments

Maria on 9 Aug 2024 at 23:37

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs

Edited: Maria about 12 hours ago

  • myData.mat

Open in MATLAB Online

Hello,

I am encountering an error in MATLAB when using the find function in my code. The error message is:

Error using find Incorrect number or types of inputs or outputs for function 'find'.

The error occurs in the following function:

load(MyData.m)

function SNR_matrix = Compute_SNR(N, numNodes, Devices,A,zeta_m_n,h0, distances,alpha,RIS_elements,arg1,arg2,Theta_node,P_v,Noise_up)

% SNR_matrix = zeros(N, numNodes, Devices);

SNR_matrix = optimexpr(N, numNodes, Devices);

for v = 1:Devices

for sb = 1:N

for node = 1:numNodes

% Find the assigned node for subtask sb of vehicle v

assigned_node = find(A(sb, :, v), 1);

% Find the covering UAV for the vehicle

covering_uav = find(zeta_m_n(v, :), 1);

if assigned_node == covering_uav

% Direct communication

SNR = sqrt(h0 * distances(v, covering_uav)^(-alpha));

SNR_matrix(sb, assigned_node, v) = (P_v*norm(SNR)^2)/Noise_up;

else

% Indirect communication through RIS

% Extract channel gain from vehicle to covering UAV

channel_gains_vehicle_to_covering = zeros(RIS_elements, 1);

for k = 1:RIS_elements

channel_gains_vehicle_to_covering(k) = arg1(v, covering_uav, k);

end

% Extract channel gain from covering UAV to assigned node

arg2_covering_to_assigned = zeros(RIS_elements, 1);

for k = 1:RIS_elements

arg2_covering_to_assigned(k) = arg2(covering_uav, assigned_node, k);

end

h_m_to_n_prime_RIS_phase_shifted = Theta_node{covering_uav} * channel_gains_vehicle_to_covering;

effective_channel = h_m_to_n_prime_RIS_phase_shifted .* arg2_covering_to_assigned;

effective_power = norm(effective_channel)^2;

% Calculate SNR

SNR = P_v * effective_power / Noise_up;

SNR_matrix(sb, assigned_node, v) = SNR;

end

end

end

end

end

and ''A'' is my optimization variable

A = optimvar('A', N, NumNodes, Devices, 'Type', 'integer', 'LowerBound', 0, 'UpperBound', 1);

Unrecognized function or variable 'N'.

How can I resolve this error? I have used fcn2optimexpr but the SNR expression becomes non-linear.

Is there a solution to avoid using find and keep the SNR linear?

Any Help will be appreciated.

Thanks in advance!

9 Comments

Show 7 older commentsHide 7 older comments

Torsten on 9 Aug 2024 at 23:59

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234424

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234424

Edited: Torsten on 10 Aug 2024 at 0:01

Open in MATLAB Online

How do you use "Compute_SNR" in your code ?

Somehow like

SNR_matrix = fcn2optimexpr(@Compute_SNR,K, N, M, A, z,...)

after defining all the inputs for the function ?

Maria on 10 Aug 2024 at 0:25

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234434

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234434

Open in MATLAB Online

Thank you for responding me.

Just i used a simple call

SNR_matrix = Compute_SNR(K, N, M, A, z,...);

Torsten on 10 Aug 2024 at 10:11

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234549

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234549

That's not possible. You must define a function that can be evaluated using optimization variables - thus you must define the SNR_matrix as output of a fcn2optimexpr.

Maria on 10 Aug 2024 at 10:32

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234559

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234559

Thank u again.

Yes this time i didn't get the same error, should i apply it for each function that contains my optimization variable A??

Torsten on 10 Aug 2024 at 13:25

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234584

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234584

Edited: Torsten on 10 Aug 2024 at 13:43

If you have a function with optimization variables as input and there are operations/functions within this function that cannot be applied to optimization variables (like "find" in the case posted), you have to use "fcn2optimexpr". I think @Walter Roberson gave you a link where the possible operations on optimization variables (without using "fcn2optimexpr") are listed.

Maria on 10 Aug 2024 at 16:34

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234684

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234684

yes, i understand now. Thank you very much.

Maria on 10 Aug 2024 at 19:17

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234799

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234799

Open in MATLAB Online

Hello @Torsten

Could you please help me to solve this issue.

I need to linearize some constraints to use the linprog solver. The problem involves constraints with max functions and products of variables, which introduce nonlinearity.

Error with find Function in MATLAB: "Incorrect number or types of i... (9)

Here is a portion of my code where I define the variables and constraints:

T_wait = optimvar('T_wait', K, N, M);

T_cmpl = optimvar('T_cmpl', K, N, M);

% Auxiliary variable for max function

z = optimvar('z', K, N, M);

% Define constraints

for m = 1:M

for n = 1:K

for k = 1:N

% Non-negativity constraint

prob.Constraints.(['constr_wait_nonneg_' num2str(m) '_' num2str(n) '_' num2str(k)]) = T_wait(n, k, m) >= 0;

% Constraints for max function

prob.Constraints.(['constr_wait_max_' num2str(m) '_' num2str(n) '_' num2str(k)]) = T_wait(n, k, m) >= z(n, k, m);

prob.Constraints.(['constr_wait_zero_' num2str(m) '_' num2str(n) '_' num2str(k)]) = z(n, k, m) >= 0;

% Constraints for nested max

for k_prime = 1:N

if k ~= k_prime

% Ensure consistent dimensions

prob.Constraints.(['constr_wait_inner_' num2str(m) '_' num2str(n) '_' num2str(k) '_' num2str(k_prime)]) = ...

z(n, k, m) >= o(k_prime, k, m) * T_cmpl(n, k_prime, m) - uplink_time(n, k, m);

end

end

% Constraint: T_cmpl = T_end + T_wait

prob.Constraints.(['constr_cmpl_' num2str(m) '_' num2str(n) '_' num2str(k)]) = ...

T_cmpl(n, k, m) == End_time(n, k, m) + T_wait(n, k, m);

end

end

end

this is the error

Error using optim.problemdef.OptimizationProblem/solve

Unable to perform assignment because the left and right sides have a different number of elements.

Error in Solve_Linear_Problem (line 253)

[sol, fval, exitflag, output] = solve(prob);

Caused by:

Failure in initial user-supplied nonlinear constraint function evaluation

Torsten on 11 Aug 2024 at 0:47

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234849

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234849

Edited: Torsten on 11 Aug 2024 at 12:10

I doubt that the error message stems from the part of the code you posted.

And I wonder why you make life difficult for yourself: you relate constraint (m,n,k) with array index (n,k,m). Doesn't it confuse you ?

Maria on 11 Aug 2024 at 11:23

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234949

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#comment_3234949

A already adjust it. Thank you for your response.

Sign in to comment.

Sign in to answer this question.

Answers (1)

Walter Roberson on 10 Aug 2024 at 0:03

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#answer_1497314

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2144319-error-with-find-function-in-matlab-incorrect-number-or-types-of-inputs-or-outputs#answer_1497314

find() is not a supported operation on optimization variables.

https://www.mathworks.com/help/optim/ug/supported-operations-on-optimization-variables-expressions.html

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

Mathematics and OptimizationOptimization ToolboxProblem-Based Optimization Setup

Find more on Problem-Based Optimization Setup in Help Center and File Exchange

Tags

  • error
  • find
  • function
  • optimization

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Error with find Function in MATLAB: "Incorrect number or types of i... (13)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Error with find Function in MATLAB: "Incorrect number or types of i... (2024)
Top Articles
Satisfaction - Vinyaya - Veronica Mars (TV) [Archive of Our Own]
Like Ripples in Water - A_Strange_Twist_of_Fate
Hotels Near 6491 Peachtree Industrial Blvd
Encore Atlanta Cheer Competition
Knoxville Tennessee White Pages
What spices do Germans cook with?
Linkvertise Bypass 2023
Top Financial Advisors in the U.S.
Unlocking the Enigmatic Tonicamille: A Journey from Small Town to Social Media Stardom
Decaying Brackenhide Blanket
Xm Tennis Channel
2024 U-Haul ® Truck Rental Review
Guidewheel lands $9M Series A-1 for SaaS that boosts manufacturing and trims carbon emissions | TechCrunch
Kitty Piggy Ssbbw
Patrick Bateman Notebook
Vistatech Quadcopter Drone With Camera Reviews
Everything you need to know about Costco Travel (and why I love it) - The Points Guy
Yard Goats Score
Espn Horse Racing Results
Johnnie Walker Double Black Costco
Lisas Stamp Studio
R. Kelly Net Worth 2024: The King Of R&B's Rise And Fall
Www.dunkinbaskinrunsonyou.con
Ceramic tiles vs vitrified tiles: Which one should you choose? - Building And Interiors
How Taraswrld Leaks Exposed the Dark Side of TikTok Fame
Busted Mugshots Paducah Ky
Cable Cove Whale Watching
R Baldurs Gate 3
Aes Salt Lake City Showdown
TJ Maxx‘s Top 12 Competitors: An Expert Analysis - Marketing Scoop
Dell 22 FHD-Computermonitor – E2222H | Dell Deutschland
Town South Swim Club
Ipcam Telegram Group
Rush County Busted Newspaper
Pixel Combat Unblocked
Chadrad Swap Shop
Memberweb Bw
Diana Lolalytics
Personalised Handmade 50th, 60th, 70th, 80th Birthday Card, Sister, Mum, Friend | eBay
Tsbarbiespanishxxl
Directions To The Closest Auto Parts Store
Thor Majestic 23A Floor Plan
Craigslist Farm And Garden Reading Pa
Cocorahs South Dakota
Cuckold Gonewildaudio
What is a lifetime maximum benefit? | healthinsurance.org
Tito Jackson, member of beloved pop group the Jackson 5, dies at 70
116 Cubic Inches To Cc
Evil Dead Rise - Everything You Need To Know
F9 2385
Adams County 911 Live Incident
7 National Titles Forum
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 5963

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.