creating an array from a single user input
4 次查看(过去 30 天)
显示 更早的评论
how would i create and array from an input:
eg user inputs the number 12
array is made: 1,2,3,4,5,6,7,8,9,10,11,12
4 个评论
Dyuman Joshi
2023-3-8
Why is it "must" to define empty matrices?
%Random value
userinput = 12;%input('input nth value for fibonacci calculation:');
%define first two fibonacci values
fibonacci = [0 1];
%define first golden ratio to be infinity
ratio = 1/0;
%make a loop for the fibonacci equation starting from 3
for i = 3:(userinput+1)
fibonacci(i)=fibonacci(i-1)+fibonacci(i-2);
end
Why only till 12? And you can also club the two for loops together.
%make a loop for golden ratio values and equation
for i=2:12
ratio(i) = fibonacci(i)/fibonacci(i-1);
end
%create an array
n = 1:13;
%display final product
table1=table(n,ratio,fibonacci)
table2struct(table1)
The code is working fine.
"however im having major problems with the n"
What is the problem that you are facing?
采纳的回答
David Hill
2023-3-8
userinput = input('input nth value for fibonacci calculation:');
%define two empty matrices one for fibonacci values and one for ratios
fibonacci = [];
ratio = [];
%define first two fibonacci values
fibonacci(1) = 0;
fibonacci(2) = 1;
fibonacci(3) = 1;
%define first golden ratio to be infinity
ratio(1) = 1/0;
%make a loop for the fibonacci equation starting from 3
for i = 3:(userinput+1)
fibonacci(i)=fibonacci(i-1)+fibonacci(i-2);
end
%make a loop for golden ratio values and equation
for i=2:userinput
ratio(i) = fibonacci(i)/fibonacci(i-1);
end
%create an array
n = 1:userinput;%NEED n TO BE 1:userinput
%display final product
table1=table(n,ratio,fibonacci);
table2struct(table1)
disp(table(userinput,fibonacci,ratio))
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!