Info

This question is closed. Reopen it to edit or answer.

Math lab questions solve it

2 views (last 30 days)
Hakim
Hakim on 18 Aug 2022
Closed: Rik on 19 Aug 2022
Write a generalized math lab program to print the following pattern first 5 the 4 4 then 3 3 3 then 2 2 2 2 then 1 1 1 1 1 (it look like triangle shape)
  12 Comments
John D'Errico
John D'Errico on 19 Aug 2022
Edited: John D'Errico on 19 Aug 2022
And this just one of many reasons why answering homework questions is just a flat out bad idea on Answers. The student thinks it is ok to pester you for help, in fact, even demand help, as @Hakim is now demanding that people provide their e-mail addresses.
Even offering simple help as Rik did is bad for Answers, because it helps to create a monster, and because it encourges other monsters to think Answers is a service where we wil do their homework.
Is this homework? Or possibly even more likely, this is a test question @Hakim wants to have answered, and thinks is being subtle by declaring it NOT homework. Obviously, as there is no alternative reason why such an easy question would be so important to be answered.
I'm sorry, but this question should have been closed immediately. I would even argue that we should close the question now, even with an accepted answer. But then I would have never answered the question.
Rik
Rik on 19 Aug 2022
@John D'Errico I'll close the question.
@Hakim, stop contacting me via email. If you provide any proof you've read and understood the links I provided I may respond. If you send me another request via the contact form I will contact Mathworks staff.

Accepted Answer

Rik
Rik on 18 Aug 2022
I will not do your homework for you, but these pointers should help you.
doc for
FOR Repeat statements a specific number of times. The general form of a FOR statement is: FOR variable = expr, statement, ..., statement END The columns of the expression are stored one at a time in the variable and then the following statements, up to the END, are executed. The expression is often of the form X:Y, in which case its columns are simply scalars. Some examples (assume N has already been assigned a value). for R = 1:N for C = 1:N A(R,C) = 1/(R+C-1); end end Step S with increments of -0.1 for S = 1.0: -0.1: 0.0, do_some_task(S), end Set E to the unit N-vectors for E = eye(N), do_some_task(E), end Long loops are more memory efficient when the colon expression appears in the FOR statement since the index vector is never created. The BREAK statement can be used to terminate the loop prematurely. See also PARFOR, IF, WHILE, SWITCH, BREAK, CONTINUE, END, COLON. Documentation for for doc for
doc repmat % although you can do this part with a loop as well
REPMAT Replicate and tile an array. B = REPMAT(A,M,N) or B = REPMAT(A,[M,N]) creates a large matrix B consisting of an M-by-N tiling of copies of A. If A is a matrix, the size of B is [size(A,1)*M, size(A,2)*N]. B = REPMAT(A,N) creates an N-by-N tiling. B = REPMAT(A,P1,P2,...,Pn) or B = REPMAT(A,[P1,P2,...,Pn]) tiles the array A to produce an n-dimensional array B composed of copies of A. The size of B is [size(A,1)*P1, size(A,2)*P2, ..., size(A,n)*Pn]. If A is m-dimensional with m > n, an m-dimensional array B is returned. In this case, the size of B is [size(A,1)*P1, size(A,2)*P2, ..., size(A,n)*Pn, size(A, n+1), ..., size(A, m)]. REPMAT(A,M,N) when A is a scalar is commonly used to produce an M-by-N matrix filled with A's value and having A's CLASS. For certain values, you may achieve the same results using other functions. Namely, REPMAT(NAN,M,N) is the same as NAN(M,N) REPMAT(SINGLE(INF),M,N) is the same as INF(M,N,'single') REPMAT(INT8(0),M,N) is the same as ZEROS(M,N,'int8') REPMAT(UINT32(1),M,N) is the same as ONES(M,N,'uint32') REPMAT(EPS,M,N) is the same as EPS(ONES(M,N)) Example: repmat(magic(2), 2, 3) repmat(uint8(5), 2, 3) Class support for input A: float: double, single integer: uint8, int8, uint16, int16, uint32, int32, uint64, int64 char, logical See also BSXFUN, MESHGRID, ONES, ZEROS, NAN, INF. Documentation for repmat doc repmat Other functions named repmat codistributed/repmat InputOutputModel/repmat tall/repmat gpuArray/repmat symfun/repmat
doc fprintf
FPRINTF Write formatted data to text file. FPRINTF(FID, FORMAT, A, ...) applies the FORMAT to all elements of array A and any additional array arguments in column order, and writes the data to a text file. FID is an integer file identifier. Obtain FID from FOPEN, or set it to 1 (for standard output, the screen) or 2 (standard error). FPRINTF uses the encoding scheme specified in the call to FOPEN. FPRINTF(FORMAT, A, ...) formats data and displays the results on the screen. COUNT = FPRINTF(...) returns the number of bytes that FPRINTF writes. FORMAT is a character vector that describes the format of the output fields, and can include combinations of the following: * Conversion specifications, which include a % character, a conversion character (such as d, i, o, u, x, f, e, g, c, or s), and optional flags, width, and precision fields. For more details, type "doc fprintf" at the command prompt. * Literal text to print. * Escape characters, including: \b Backspace '' Single quotation mark \f Form feed %% Percent character \n New line \\ Backslash \r Carriage return \xN Hexadecimal number N \t Horizontal tab \N Octal number N For most cases, \n is sufficient for a single line break. However, if you are creating a file for use with Microsoft Notepad, specify a combination of \r\n to move to a new line. Notes: If you apply an integer or text conversion to a numeric value that contains a fraction, MATLAB overrides the specified conversion, and uses %e. Numeric conversions print only the real component of complex numbers. Example: Create a text file called exp.txt containing a short table of the exponential function. x = 0:.1:1; y = [x; exp(x)]; fid = fopen('exp.txt','w'); fprintf(fid,'%6.2f %12.8f\n',y); fclose(fid); Examine the contents of exp.txt: type exp.txt MATLAB returns: 0.00 1.00000000 0.10 1.10517092 ... 1.00 2.71828183 See also FOPEN, FCLOSE, FSCANF, FREAD, FWRITE, SPRINTF, DISP. Documentation for fprintf doc fprintf Other functions named fprintf dlarray/fprintf icinterface/fprintf serial/fprintf gpuArray/fprintf
  7 Comments
Hakim
Hakim on 18 Aug 2022
Plz give me your email before @
Walter Roberson
Walter Roberson on 18 Aug 2022
My email address before the @ is c6614770

More Answers (0)

Community Treasure Hunt

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

Start Hunting!