Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 解码阵列输入和while循环_Arrays_Matlab_Loops_Conditional Statements - Fatal编程技术网

Arrays 解码阵列输入和while循环

Arrays 解码阵列输入和while循环,arrays,matlab,loops,conditional-statements,Arrays,Matlab,Loops,Conditional Statements,我基本上只需要帮助在matlab中解码这段代码,每一行的意思是什么,它做什么 基本上,创建一个数组g,其中g中的每个值都是g的前一个值,但都是平方。当g达到或大于g原始值100倍时停止 g = input('Please provide an initial value: '); while ((g == 1) || (g == 0)) disp('Cannot be 0 or 1') g = input('Please provi

我基本上只需要帮助在matlab中解码这段代码,每一行的意思是什么,它做什么

基本上,创建一个数组g,其中g中的每个值都是g的前一个值,但都是平方。当g达到或大于g原始值100倍时停止

g = input('Please provide an initial value: '); 

while ((g == 1) || (g == 0))    
           disp('Cannot be 0 or 1')    
           g = input('Please provide an initial value: ');
end

i = 1; 
while ((g(i)^2)<=(100*g(1)))    
        g(i+1) = g(i)^2;    
        i = i+1; 
end

g = g'
代码要求输入一个不能为0或1的数字。然后该数字被平方,但当下一个值小于或等于初始数字的100倍时停止

例如,如果您输入2 代码将输出2、4、16,并将停止,因为下一个值是256,大于2*100=200


提前谢谢。

您可以询问代码的作用,然后解释代码的作用。我不明白你在问什么。您可以在MATLAB中键入help xxx以获取有关xxx的帮助。对代码中你不理解的每个单词都这样做,并记住!我建议,不要在这里问这样的问题,而是在调试器中单步执行代码,只需单击第一行代码的左边距,创建断点,然后运行代码。您将看到每一行的作用以及它如何更改所涉及的变量。您还可以在MATLAB命令提示符中键入部分表达式,以查看它们的作用。自己探索,不要依赖别人向你解释。这样你会学到更多。如果有一句话真的让你难堪,就在这里问一个关于这句话的问题。
g = input('Please provide an initial value: '); 
//print this out to screen ; wait for user to type in ; press enter ;
// g becomes the number that the user type in ;
while ((g == 1) || (g == 0))    
// while g equals to 1 or g equals to zero ;
    disp('Cannot be 0 or 1')    
//print this statement to screen / console ;
    g = input('Please provide an initial value: ');
//print this out to screen ; wait for user to type in ; press enter ;
// g becomes the number that the user type in ;
end
// end the while loop ;
i = 1; 
// let i be the counter / index ; only to control the looping ;
while ((g(i)^2)<=(100*g(1)))    
// while i-th element of g is less than or equal to 100 times first element of g ;
    g(i+1) = g(i)^2;    
// i+1-th element of g is define as i-th element of g squared ;
// note that g actually becomes an array when this statement runs ;
    i = i+1; 
// i is define as i plus 1 ; meaning if i was 1 i is now 2 ;
end
// end the while loop ;
g = g'
// g become g transpose ; Matlab default to row array so this make it column array ;