i need a nested for loop matlab pattern following this code :
1
121
12321
1234321
123454321

1 Kommentar

Jan
Jan am 25 Mär. 2018
This sounds like a homework. It would not be useful if the forum solves your homework. So please post, what you have tried so far and ask a specific question.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Andrei Bobrov
Andrei Bobrov am 22 Mär. 2018
Bearbeitet: Jan am 25 Mär. 2018

0 Stimmen

ii = (1:5)';
out = arrayfun(@(ii)[1:ii,ii-1:-1:1],ii,'un',0);
or
n = 5;
A = [triu(ones(1,n).*(1:n)');flip(triu(ones(1,n).*(1:n)',1),1)];
t = A ~= 0;
B = zeros(n*2-1,n);
k = sort(t,'descend');
B(k(any(k,2),:)) = A(t);
out = B';
Jan
Jan am 25 Mär. 2018

0 Stimmen

A "nested for loop" is accurate enough to start:
n = 5:
for i1 = 1:n
for i2 = ???
...
end
end
Now insert, what you want to appear. fprintf('%i', v) and fprintf('\n') will help.

3 Kommentare

salah berra
salah berra am 27 Mär. 2018
tell me exactly
Walter Roberson
Walter Roberson am 27 Mär. 2018
You appear to be asking that someone gives you a complete answer to a homework question.
Jan
Jan am 27 Mär. 2018
Bearbeitet: Jan am 27 Mär. 2018

@salah berra: I assume also, that this is a homework question. Then posting a solution is to useful, because it would be cheating, if you submit it as your solution.

All you have to do is to replace the "???" in the inner loop by some meaningful limits, to insert "fprintf('%i', v')" with replacing the "v" by one of the loop indices, and a "fprintf('\n')" as a line break. Then you have the code for:

1
12
123
1234

But now it should be easy to get the trailing part of the lines also. Example:

for i3 = 4:-1:1
  disp(i3)
end

I'm convinced, that you are able to do this and then submit a solution written by your own.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Performance finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 22 Mär. 2018

Bearbeitet:

Jan
am 27 Mär. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by