how to delete this error?

1 vue (au cours des 30 derniers jours)
zero
zero le 14 Fév 2018
function y = dd(x)
% x is a vector
% We create an output vector of only 0 (our default value)
y = zeros(1, length(x));
% We find indexes of input values equal to 0,
% and make them 1
y(find(x==0)) = 1;
function y = dd(x)
Error: Function definitions are not permitted in this context.

Réponses (3)

Jos (10584)
Jos (10584) le 14 Fév 2018
or even shorter
y = ~x
  1 commentaire
Jan
Jan le 14 Fév 2018
+1, shortest.

Connectez-vous pour commenter.


Jan
Jan le 14 Fév 2018
Modifié(e) : Jan le 14 Fév 2018
Error: Function definitions are not permitted in this context.
This means, that you either try to create this function in the command window, or inside a script while using Matlab < R2016b.
Function can be defined in the editor inside an "M-function-file" (an M-file which starts with the keyword "function"), or with using a modern Matlab version in a "M-script-file" (an M-file not starting with "function").
By the way: As Birdman has mentioned already, the find() can be omitted:
y = zeros(1, length(x));
y(x==0) = 1;
Or even shorter:
y = (x == 0);

Image Analyst
Image Analyst le 15 Fév 2018
You are defining a function dd(x), but then at line 9 of that function you try to define a nested function with the very same name, dd(), nested inside the dd() function. That is, if you're ending your functions with the "end" keyword.
function y = dd(x)
% x is a vector
% We create an output vector of only 0 (our default value)
y = zeros(1, length(x));
% We find indexes of input values equal to 0,
% and make them 1
y(find(x==0)) = 1;
function y = dd(x)
Error: Function definitions are not permitted in this context.
Why??? You can't do that. You already have a dd function, why would you want another one of the same name to be defined inside of it???
Or, if you're defining functions without the "end" keyword, then you're not nesting but you're defining two functions with the same name in the same m-file. Again, Why???? You can't do that.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by