# combining results into a single matrix

2 Ansichten (letzte 30 Tage)
Io7 am 22 Sep. 2021
Kommentiert: Io7 am 22 Sep. 2021
Code:
h = [1 2 3 4 5];
for i = 2:4
First_Matrix = [h(i-1) h(i) h(i+1) ]
end
Result:
First_Matrix =
1 2 3
First_Matrix =
2 3 4
First_Matrix =
3 4 5
I want help combining the result into a single matrix which is from 3x5 from h1 to h5, I want the answer to look like.
First_Matrix = [ 1 2 3 0 0; 0 2 3 4 0; 0 0 3 4 5]
and I don't want to hard code it. please help! I would really appreciate it if somone can help me.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Matt J am 22 Sep. 2021
Bearbeitet: Matt J am 22 Sep. 2021
One way.
h = [1 2 3 4 5];
n=numel(h);
m=n-2;
First_Matrix=toeplitz([1,zeros(1,m-1)] ,[1 1 1 zeros(1,n-3)]).*h
First_Matrix = 3×5
1 2 3 0 0 0 2 3 4 0 0 0 3 4 5
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Io7 am 22 Sep. 2021
really appreciate it

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by