Inserting a row into the matrix in each iterations

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 commentaires

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.
Ok i will use the diff but if i use diff method how can i approach with the code
Rik
Rik le 18 Mar 2021
You didn't tell me what your goal is, so you will have to read the documentation yourself.
Hi i have read about the diff method, it seems it is decreasing the matrix increase of increasing the matrix, i have also tried your method, it just runs n times where the matrix is not increasing in coloum/row, it remains the same for all the iterations.
To change it to coloum i just
Dia_1=[1;h(i:1)]; is this right?
I believe Rik is suggesting that your loop is doing what the diff function does.
h = diff(x);
Rik
Rik le 18 Mar 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).

Connectez-vous pour commenter.

 Réponse acceptée

Juan Diego Castillo Cruz
Juan Diego Castillo Cruz le 18 Mar 2021
Modifié(e) : Juan Diego Castillo Cruz le 18 Mar 2021
I understand that you want to make the variable grow every iteration. Then:
Dia_1=[]; %Create a array
for i=1:n-1
h(i)=x(i+1)-x(i);
iter_h=iter_h+1;
Dia_1 = [Dia_1 h(i)] %This insert columns
end

4 commentaires

Adam Danz
Adam Danz le 18 Mar 2021
Modifié(e) : Adam Danz le 19 Mar 2021
Yea, but it's generally a bad idea to dynamically increase variable sizes. 1) it's slow, 2) most of the time the final size of the array is known before its produced so why let the loop determine its size?
A better implementation of your answer would be,
Dia_1 = nan(1,n-1): % for a row vector
for i = 1:n-1
. . .
Dia_1(i) = h(i);
end
Hi Juan Diego i have another question, yes you have answer my question to insert a new row, but if i have a specific equations i like to put inside the new coloum, how can i do so?
Your codes suggest that each new coloum will place the new h value at each iterations, my main goals was to have a h(i) as the first and then the new coloumn is 2(h(i+1)+h(i)) this is my main objective how can implement this into the code?
Do i do it as Dia_1=[Dia_1 h(i), Dia_1*(h(i+1)+h(i))]
Can i do it like this?
Rik
Rik le 19 Mar 2021
Did you already do a basic Matlab tutorial? It sounds like that would be a good time investment.
I assume that h is a matrix then:
h = ones(m,n-1);
Dia_1 = ones(m,n)
for i=1:n-1
h(:,i) = [i 2*i 3*i]; %For example this vector
Dia_1(:,i+1)=h(:,i).*Dia_1(:,i);
end
This was my understanding.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by