8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
gravy am 29 Jan. 2024
Kommentiert: gravy am 29 Jan. 2024
Akzeptierte Antwort: Voss
In MATLAB Online öffnen
Good evening, I need to collect data into an array with accumulation.
For example, there are 9 time values t=9, the array should look like this (0 t1 t1+t2 t1+t2+t3 and...) I want to do this in a loop so that it would be possible to regulate the number of t values.
So far I've thought of this, but it doesn't work correctly:
index=1;
n=10;
c=[];
while index<10
t = b(index)+b(index+1)
if t>b(index) && index<10
t1=0;
t1 = t + b(index)
index=index+1;
end
c=[c t1];
end
Unrecognized function or variable 'b'.
c
3 Kommentare 1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Dyuman Joshi am 29 Jan. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2075881-accumulating-data-in-a-loop-while#comment_3046371
Few things -
"b" is not defined.
You initialize "t1" as zero, but then over-write in the next line, which does not makes sense to me.
What are you trying to do? What is the objective? What is the code suppose to do?
You have added the tag "database" for this question - How does a database come into this question?
gravy am 29 Jan. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2075881-accumulating-data-in-a-loop-while#comment_3046376
"b" this is part of the previous code
gravy am 29 Jan. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2075881-accumulating-data-in-a-loop-while#comment_3046396
Bearbeitet: Walter Roberson am 29 Jan. 2024
In MATLAB Online öffnen
This is what it looks like in simple form
%t0=0;
%t1=delt0;
%t2=delt0+delt1;
%t3=delt0+delt1+delt2;
%t4=delt0+delt1+delt2+delt3;
%t5=delt0+delt1+delt2+delt3+delt4;
%t6=delt0+delt1+delt2+delt3+delt4+delt5;
%t7=delt0+delt1+delt2+delt3+delt4+delt5+delt6;
%t8=delt0+delt1+delt2+delt3+delt4+delt5+delt6+delt7;
%t9=delt0+delt1+delt2+delt3+delt4+delt5+delt6+delt7+delt8;
%t10=delt0+delt1+delt2+delt3+delt4+delt5+delt6+delt7+delt8+delt9;
%t = [t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10];
I want to automate this process and not write new variables myself every time.
Here is the full code, maybe it will become clearer:
X = linspace(0,100,12);
for x = X
if (0<=x) && (x<5.5)
y1=((x-0)/(5.5-0))*(7.74-9.19)+9.19;
end
if (5.5<=x) && (x<14.5)
y2=((x-5.5)/(14.5-5.5))*(6.45-7.74)+7.74;
end
if (14.5<=x) && (x<22.5)
y3=((x-14.5)/(22.5-14.5))*(5.56-6.45)+6.45;
end
if (22.5<=x) && (x<30)
y4=((x-22.5)/(30-22.5))*(4.79-5.56)+5.56;
end
if (30<=x) && (x<43.5)
y5=((x-30)/(43.5-30))*(3.43-4.79)+4.79;
end
if (43.5<=x) && (x<49)
y6=((x-43.5)/(49-43.5)).*(3.04-3.43)+3.43;
end
if (49<=x) && (x<56)
y7=((x-49)/(56-49))*(2.39-3.04)+3.04;
end
if (56<=x) && (x<64)
y8=((x-56)/(60-56)).*(0.8-2.39)+2.39;
end
if (64<=x) && (x<73)
y9=((x-60)/(70-60))*(-1.98-0.8)+0.8;
end
if (73<=x) && (x<83)
y10=((x-70)/(80-70))*(-3.88+1.98)-1.98;
end
if (83<=x) && (x<93)
y11=((x-80)/(90-80))*(-5.45+3.88)-3.88;
end
if (93<=x) && (x<=100)
y12=((x-90)/(100-90))*(-6.74+5.45)-5.45;
end
end
Y=[y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 y12];
X = linspace(0,100,12);
v = [ 0:10:100 ];
delv=10;
dzeta = 120;
w0=0;
v1=0;
a=[];
while v1<100 %&& w0<3
fycp = abs(((interp1(X,Y,v1)-w0)+((interp1(X,Y,v1+delv))-w0)))/2;
v1=v1+10;
% w0=w0+1;
a=[a fycp];
end
b=[];
index=1;
while index<11
delt = delv/(dzeta*a(index));
index=index+1;
b=[b delt];
end
index=1;
n=10;
c=[];
while index<10
t = b(index)+b(index+1)
if t>b(index) && index<10
t1=0;
t1 = t + b(index)
index=index+1;
end
c=[c t1];
end
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Akzeptierte Antwort
Voss am 29 Jan. 2024
In MATLAB Online öffnen
t = rand(1,9) % 9 random t values
t = 1×9
0.7787 0.5803 0.4362 0.3903 0.0329 0.8313 0.1897 0.7956 0.7963
c = c*msum([0 t]) % c = [0, t(1), t(1)+t(2), t(1)+t(2)+t(3), etc.]
c = 1×10
0 0.7787 1.3590 1.7951 2.1854 2.2183 3.0496 3.2394 4.0349 4.8312
4 Kommentare 2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
gravy am 29 Jan. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2075881-accumulating-data-in-a-loop-while#comment_3046416
You yourself assigned indices 1,2,3, etc., but I want the program to add the next value to the previous one, and I could regulate the number of “indices” myself.
Voss am 29 Jan. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2075881-accumulating-data-in-a-loop-while#comment_3046421
In MATLAB Online öffnen
It seems like you want to cumulatively sum the t where the next b is positive, based on this:
t = b(index)+b(index+1)
if t>b(index) && index<10
That is, given that t = b(index)+b(index+1), then t>b(index) implies b(index+1)>0.
b = randn(1,10) % 10 random b values, some of which are negative
b = 1×10
-0.7297 -0.1824 0.8041 1.0788 -0.7439 -0.8178 1.7741 0.2454 -1.5478 -1.7278
t = rand(1,9) % 9 random t values
t = 1×9
0.2471 0.7733 0.9757 0.7641 0.3566 0.9937 0.5369 0.1627 0.0781
idx = find(b > 0);
if ~isempty(idx) && idx(1) == 1
idx(1) = [];
end
c = c*msum([0 t(idx-1)]) % cumulatively sum the elements of t where the next b is positive
c = 1×5
0 0.7733 1.7490 2.7427 3.2796
gravy am 29 Jan. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2075881-accumulating-data-in-a-loop-while#comment_3046431
Bearbeitet: gravy am 29 Jan. 2024
still not the same, it should look like you wrote before:
t = rand(1.9) % 9 random t values
c = c*msum([0 t]) % c = [0, t(1), t(1)+t(2), t(1)+t(2)+t(3), etc.]
But the summation in the array should be done automatically, I want to change the value of t, but not rewrite the array every time. There will be a graph with "b" and "c" and their number in the array should converge.
I tried to set some kind of condition, but I couldn’t figure it out yet.
gravy am 29 Jan. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2075881-accumulating-data-in-a-loop-while#comment_3046446
I'm an idiot, your previous answer was correct. Thanks a lot
Melden Sie sich an, um zu kommentieren.
Weitere Antworten (0)
Melden Sie sich an, um diese Frage zu beantworten.
Siehe auch
Kategorien
MATLABLanguage FundamentalsMatrices and ArraysMatrix Indexing
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!
Es ist ein Fehler aufgetreten
Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asien-Pazifik
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Kontakt zu Ihrer lokalen Niederlassung