103 views (last 30 days)
Show older comments
Maria on 9 Aug 2024 at 23:37
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
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
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
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
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
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
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
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
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.
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
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
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
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
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.
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