Error in the anonymous function
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!