Effacer les filtres
Effacer les filtres

Not enough input arguments

4 vues (au cours des 30 derniers jours)
Ngoc
Ngoc le 31 Mai 2024
Commenté : Ngoc le 1 Juin 2024
Hi i'm new to MatLab. Why I can't get the values from matrix x in function MyInput() to use in another function?
function Calculate(x)
MyInput();
y = zeros(1,6);
for j=2:6
y(1,j) = x(1,j)^j;
j=j+1;
end
end
function x = MyInput()
x = zeros(1,6);
x(1,1) = 2;
for i=2:6
x(1,i)=x(1,i-1)+1;
i=i+1;
end
end
The command window says : ' Not enough input arguments.'. It seems to give this error for line 5 but I'm not sure why.
Thanks.

Réponse acceptée

Stephen23
Stephen23 le 31 Mai 2024
Modifié(e) : Stephen23 le 31 Mai 2024
"Why I can't get the values from matrix x in function MyInput() to use in another function?"
Basically because you did not call MyInput with an output argument.
Note that it makes little sense to define Calculate with an input argument... and then completely ignore that input argument by redefining it on the first line of code. Much better to use that input argument.
Note that you should not increment the index i=i+1 and j=j+1 at the end of the for loops: the for operator will simply redefine those values at the start of each iteration, so those lines serve absolutely no purpose. Get rid of them.
vec = MyInput()
vec = 1x6
2 3 4 5 6 7
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
out = Calculate(vec)
out = 1x6
0 9 64 625 7776 117649
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function y = Calculate(x)
y = zeros(1,6);
for j=2:6
y(1,j) = x(1,j)^j;
end
end
function x = MyInput()
x = zeros(1,6);
x(1,1) = 2;
for i=2:6
x(1,i)=x(1,i-1)+1;
end
end
  2 commentaires
Stephen23
Stephen23 le 31 Mai 2024
Modifié(e) : Stephen23 le 31 Mai 2024
A simpler MATLAB approach:
vec = 2:7
vec = 1x6
2 3 4 5 6 7
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
out = [0,vec(2:end).^(2:6)]
out = 1x6
0 9 64 625 7776 117649
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Ngoc
Ngoc le 1 Juin 2024
Thank you so much!!!😊😊😊

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

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

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by