Info
This question is closed. Reopen it to edit or answer.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Math lab questions solve it
1 view (last 30 days)
Show older comments
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
Rik
on 18 Aug 2022
For future questions:
You can find guidelines for posting homework on this forum here. If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks). If your main issue is with understanding the underlying concept, you may consider re-reading the material you teacher provided and ask them for further clarification.
If you don't follow this advice, the chance of someone closing your question is substantial.
Rik
on 19 Aug 2022
There is no problem posting a question, but you see what happens.
What would happen if I gave you my email address as you requested? Will you follow the advice I gave you in my comment? Somehow I doubt it. I don't do private consulting, nor do most people here. Yair Altman is one of the few exceptions I'm aware of.
If you want to learn Matlab, you need to put in some effort. Follow the advice I gave you in the previous comment, and you will find most people here are willing to help you.
Rik
on 19 Aug 2022
Why don't you respond to what I ask in my comment?
What were you planning to send me? Anything relevant to this question can be posted here. I don't see why you would need to send me anything by email. You already asked me for my email address via the contact form. Repeating the request in a comment does nothing useful.
Rik
on 19 Aug 2022
If you ignore half of my comment, why should I respond to your comment?
You can find guidelines for posting homework on this forum here. If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks). If your main issue is with understanding the underlying concept, you may consider re-reading the material you teacher provided and ask them for further clarification.
Rik
on 19 Aug 2022
Come on. Read my comments. Stop sending me emails. If you want to show me a pdf, attach it to a comment or question.
If your English is not good enough to understand me, use something like Google Translate to translate it to your own language.
I'm not a private consultant. I will not give you my email address.
Please read the advice I gave you.
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
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
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
Image Analyst
on 18 Aug 2022
Walter Roberson
on 18 Aug 2022
This task is common to be given as an early assignment in MATLAB classes. It might not be homework for you but it is certainly homework for other people, and we are not going to post a solution.
This task is intended to be very basic. It can be done one character at a time knowing only how to write for loops, how to display a single character, and how to code the displaying of end of line. Or it can be done one line at a time knowing how to write for loops and how to store into an array and how to display an array.
Walter Roberson
on 18 Aug 2022
Image Analyst uses a public junk email address that anyone can read and which deletes all messages after one hour. He almost never looks there. So even if you managed to find the address, chances are slim that he will ever read the message.
I would be unlikely to respond to an email about this task, unless it would be to point you to resources to learn matlab.
I would suggest that for your learning purposes that you first implement the program in any procedural programming language that you are already comfortable with, and then figure out the closest matlab equivalent to each statement.
Walter Roberson
on 18 Aug 2022
My email address before the @ is c6614770
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)