Error in the anonymous function

3 views (last 30 days)
shane watson
shane watson on 26 Jan 2024
Commented: shane watson on 29 Jan 2024
Sorry for the basic question, but a help is much appreciated.
I'have following code below and I'm trying to select randomly single value for two or more variables from a vector of 1x24. Self is the vector, here the error appears as follwing. I also find the syntax error, however, I can't see in problem from myside.
S_r = length (Self);...
Error: Incorrect use of '=' operator. To assign a value to a variable, use '='. To compare values for equality, use
'=='.
P_k = @(Self, ny)(...
S_r = length (Self,);...
R_p = randperm(S_r);...
D_i = R_p(1:ny);...
C_a(D_i)...
);
In addition, I also see syntax errors e.g., invalid syntax at '='.A'(' on line 26 might be missing')' line 26 is the first line.
And on the last line it is parse error at ')': usage might invalid syntax error
I can also attached a simple example of the whole secnario below.
Define A as a 1x24 vector
A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...
21, 22, 23, 24];
% Define an anonymous function that returns a random subset of n values from A
sample = @(A, n) ( ... % Use @ to define an anonymous function
N = length(A); ... % Get the size of A
P = randperm(N); ... % Generate a random permutation of the indices of A
I = P(1:n); ... % Select the first n indices from the permutation
A(I) ... % Access the corresponding values of A
);
% Define the number of iterations
iterations = 24;
% Loop for each iteration
for i = 1:iterations
% Sample two values from A and assign them to B and C
[B, C] = sample(A, 2);
% Print B and C
fprintf('Iteration %d: B = %d, C = %d\n', i, B, C);
end

Accepted Answer

VBBV
VBBV on 26 Jan 2024
Edited: VBBV on 26 Jan 2024
% Define A as a 1x24 vector
AA = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...
21, 22, 23, 24];
n = 2; % change the value here
N = length(AA); % Get the size of A
% Define the number of iterations
iterations = 24;
% Loop for each iteration
for i = 1:iterations
P = randperm(N); ... % Generate a random permutation of the indices of A
I = P(1:n); ... % Select the first n indices from the permutation
AA(I); % Access the corresponding values of A
% Define an anonymous function that returns a random subset of n values from A
sample = @(A, n) [N P I A]; %... % Use @ to define an anonymous function
fprintf('Iteration %d: \n', i)
% Sample two values from A and assign them to B and C
sample(AA(i), n)
% Print B and C
end
Iteration 1:
ans = 1×28
24 20 12 3 8 13 22 17 5 2 10 14 24 18 11 9 15 23 16 19 7 1 6 4 21 20 12 1
Iteration 2:
ans = 1×28
24 16 17 10 6 24 20 13 9 1 3 18 21 22 12 4 7 15 2 23 8 11 5 19 14 16 17 2
Iteration 3:
ans = 1×28
24 8 10 3 6 13 11 17 19 4 15 12 14 22 9 20 18 21 23 16 7 1 5 24 2 8 10 3
Iteration 4:
ans = 1×28
24 15 14 24 10 19 23 21 17 13 12 18 22 1 5 6 4 9 16 3 20 2 8 7 11 15 14 4
Iteration 5:
ans = 1×28
24 24 1 23 12 5 8 15 9 17 6 19 21 14 13 16 4 11 2 18 22 3 20 10 7 24 1 5
Iteration 6:
ans = 1×28
24 12 13 24 1 11 8 22 10 15 6 19 4 7 20 16 5 21 23 14 17 2 9 18 3 12 13 6
Iteration 7:
ans = 1×28
24 13 1 9 6 18 19 17 23 15 8 7 21 2 11 12 3 14 10 5 20 24 22 16 4 13 1 7
Iteration 8:
ans = 1×28
24 6 4 14 17 3 10 16 22 20 5 13 12 2 23 18 11 24 8 9 21 7 1 15 19 6 4 8
Iteration 9:
ans = 1×28
24 6 9 13 23 5 21 1 2 14 15 7 22 12 16 10 24 3 20 19 17 11 18 8 4 6 9 9
Iteration 10:
ans = 1×28
24 12 19 2 4 6 8 3 16 11 14 18 23 17 10 21 15 5 13 22 24 20 7 9 1 12 19 10
Iteration 11:
ans = 1×28
24 5 8 7 21 6 2 3 22 4 15 10 13 12 19 9 1 14 23 16 24 20 17 11 18 5 8 11
Iteration 12:
ans = 1×28
24 12 5 21 6 24 9 7 1 11 19 22 10 2 16 14 3 13 18 15 8 23 4 17 20 12 5 12
Iteration 13:
ans = 1×28
24 18 16 19 11 3 14 20 24 9 17 22 7 5 8 4 12 15 6 1 10 23 2 21 13 18 16 13
Iteration 14:
ans = 1×28
24 6 5 11 8 3 22 17 23 24 2 13 14 16 15 9 20 7 19 18 4 10 21 1 12 6 5 14
Iteration 15:
ans = 1×28
24 23 24 2 1 21 12 13 19 20 14 8 22 18 4 3 6 10 16 9 5 15 11 17 7 23 24 15
Iteration 16:
ans = 1×28
24 8 18 23 21 1 22 20 3 10 15 11 4 6 5 17 7 13 14 19 12 9 16 2 24 8 18 16
Iteration 17:
ans = 1×28
24 7 17 2 12 24 23 13 4 3 19 15 21 14 22 16 20 1 5 11 18 6 8 10 9 7 17 17
Iteration 18:
ans = 1×28
24 18 10 5 24 15 12 19 11 16 22 8 6 14 13 4 7 1 2 21 9 3 23 17 20 18 10 18
Iteration 19:
ans = 1×28
24 11 5 23 12 15 16 3 7 10 22 9 19 6 20 24 14 13 17 8 18 1 21 4 2 11 5 19
Iteration 20:
ans = 1×28
24 19 21 14 12 13 20 10 15 24 6 11 1 17 8 2 18 16 4 7 22 23 5 3 9 19 21 20
Iteration 21:
ans = 1×28
24 12 4 1 19 2 17 10 22 23 11 6 16 5 21 13 9 7 3 18 14 24 8 15 20 12 4 21
Iteration 22:
ans = 1×28
24 23 11 16 8 19 24 22 2 14 6 10 1 5 9 4 15 17 13 3 20 18 12 21 7 23 11 22
Iteration 23:
ans = 1×28
24 5 8 19 18 23 24 1 21 3 9 16 7 12 17 15 14 2 20 10 22 13 6 4 11 5 8 23
Iteration 24:
ans = 1×28
24 10 8 15 17 23 16 20 19 18 7 2 1 3 14 4 11 5 9 12 24 6 21 13 22 10 8 24
  3 Comments
VBBV
VBBV on 26 Jan 2024
' ( ' and ' ) ' are parenthesis used for named function definitions. However, for anonymous function definitons they may not be needed.
shane watson
shane watson on 29 Jan 2024
@VBBV, Thanks for your response, a bit late to respond you, however, I'm wondering that why matrix size increased from 1x24? And you also, you didn't assign values to B and C. I already accepted your answer, however, could you please respond to me. thanks in advanced

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!