creating an array from a single user input

조회 수: 4 (최근 30일)
Jakub
Jakub 2023년 3월 8일
답변: David Hill 2023년 3월 8일
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
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)
table1 = 1×3 table
n ratio fibonacci ___________ ___________ ___________ 1×13 double 1×12 double 1×13 double
table2struct(table1)
ans = struct with fields:
n: [1 2 3 4 5 6 7 8 9 10 11 12 13] ratio: [Inf Inf 1 2 1.5000 1.6667 1.6000 1.6250 1.6154 1.6190 1.6176 1.6182] fibonacci: [0 1 1 2 3 5 8 13 21 34 55 89 144]
The code is working fine.
"however im having major problems with the n"
What is the problem that you are facing?
Jakub
Jakub 2023년 3월 8일
clc
%user input
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:13;
%display final product
table1=table(n,ratio,fibonacci);
table2struct(table1)
disp(table(userinput,fibonacci,ratio))
so this is the fixed script, ive changed for i = 2:userinput so now whatever is inputted then that is what the ratio will go up to, will lump the for loops together. however what id like is for the n to be an array dependant on whatever has been input so if i input 25 then the ratio goes from 1-25 and so does fibonacci however n stays at 13.
However id like the n value to display 1,2,3,4,5,6,7,8...until user input has been reached.

댓글을 달려면 로그인하십시오.

채택된 답변

David Hill
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개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by