create array and assign the values to proper locations

1 Ansicht (letzte 30 Tage)
Ali Tawfik am 2 Jun. 2020
Bearbeitet: madhan ravi am 2 Jun. 2020
I would like store each value and add them to the previous to create an array ...
expected result should be= [-8 0 0 0 0 0 0 8], however my output is [-8 0 0 0 0 0 0 0] seems the values does NOT store and change everytime ??
clear all; clc;
t=16;
t_all=[8 0 0 0 0 0 0 8];
for i=1:8
z_all=-t/2; % I want have this number as the first element in the new array
z_all(i)=z_all+t_all(i)
end
z_all(1)=-t/2; z_all(2)=z_all(1)+t_all(1); z_all(3)=z_all(2)+t_all(2); z_all(4)=z_all(3)+t_all(3) and so on.. until
z_all(9)=z_all(8)+t_all(8)....
Hope anyone can help!!
4 Kommentare2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
Ali Tawfik am 2 Jun. 2020
Actually, I am trying and I am kinda understand my loop not correct, but I do not know how to initialize the new vector with a specfic number then add to previous ?
hope you guide me
Ali Tawfik am 2 Jun. 2020
Also, I understand I have to learn more by trying. bymyself..So I posted the question asking not only for answer but for even suggestions, not just comments without any useful suggestions

Melden Sie sich an, um zu kommentieren.

Antworten (1)

madhan ravi am 2 Jun. 2020
Bearbeitet: madhan ravi am 2 Jun. 2020
clear all; clc;
t=16;
t_all=[8 0 0 0 0 0 0 8];
z_all = zeros(size(t_all)); % preallocate
z_all(1) = -t/2;
for ii = 2:8
z_all(ii) = z_all(ii-1)+t_all(ii-1)
end
z_all % I leave the rest for you to analyse the result
0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing 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