matlab help, please I am learning matlab by myself. I got trouble when creating fibonaccing sequence. the output shows 11235813..... in a whole thing like this. I don't know how to separate them like 1 1 2 3 5 8 13 21.....
Here is the code I used fprintf('The Fibonacci sequence to %d terms is \n',N) fprintf('%g',fib) fprintf('/n')
end each line with a \n (not /n) fprintf('%g\n',fib) will do the trick. For simple output, you can also use disp(); A crude code would be: a=0; b=1; disp(a); disp(b); for i=1:6 c=a+b; disp(c); a=b; b=c; end; which gives the output of: 0 1 1 2 3 5 8 13
I wonder why do we have to use fprintf while we can work on it directly?
Oh, wait, if you want to print them all on one single line, then you just have to put an extra space after the %g fprintf('The Fibonacci sequence to %d terms is \n',N) fprintf('%g ',fib) fprintf('/n') or separate with a semi-colon + a space (or any other character(s) by doing: fprintf('The Fibonacci sequence to %d terms is \n',N) fprintf('%g; ',fib) fprintf('/n')
oh, got you. Thanks a lot
fprintf gives you many formatting options, control the number of decimal places, or spacing, or carriage returns, different kinds of numeric or string outputs, etc. It's fascinating! :)
Yes, it is. thanks again :)
You're welcome! :)
Join our real-time social learning platform and learn together with your friends!