Inserting a row into the matrix in each iterations
Ältere Kommentare anzeigen
Hi there i like to ask how can i insert a new row/colum into the matrix with each increasing of the iterations
for i=1:n-1
h(i)=x(i+1)-x(i);
iter_h=iter_h+1;
Dia_1= [1 h(i) h(i+1)]
end
My interest is to add one coloum into Dia_1 for each iterations, for example Dia_1=[1 h(1) h(2) h(3) and so on for every new i, the h(i) will pop up here
How can i do so?
6 Kommentare
Rik
am 18 Mär. 2021
Dynamically growing a variable is generally a bad idea. It sounds like you simply need to do this
Dia_1=[1;h(1:i)];
% ^ because you said you wanted to add rows, not columns
If the code you posted is the real code instead of just an example, you probably should be using diff instead.
Mark Loui
am 18 Mär. 2021
Rik
am 18 Mär. 2021
You didn't tell me what your goal is, so you will have to read the documentation yourself.
Mark Loui
am 18 Mär. 2021
Adam Danz
am 18 Mär. 2021
h = diff(x);
Rik
am 18 Mär. 2021
That is indeed what I meant: if you want to calculate x(i+1)-x(i) for all positions of x, then you can simply do diff(x) to get the same result.
I have no clue what you are trying to achieve, so I can't give you a real solution. What do you want to do with Dia_1?
If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks).
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Sparse Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!