Adding specified loop outputs

3 vues (au cours des 30 derniers jours)
S
S le 28 Mar 2024
Commenté : S le 11 Avr 2024
I am trying to make a figure which plots the sum of 3 specific loop outputs. output is the different outputs of the loop and I am trying to get the sum of output 1, 2, and 3. This is what I have:
figure
Sum1=sum(output(1:3));
plot(t,Sum1)
But I am just getting a blank graph and Sum1 for some reason has a value of 0 in my workplace. I am not sure which part of what I am doing is incorrect. Thank you for your time!
  4 commentaires
S
S le 30 Mar 2024
>> size(output)
ans =
5 2000
Torsten
Torsten le 30 Mar 2024
Modifié(e) : Torsten le 30 Mar 2024
Example:
output = rand(5,2000)
output = 5×2000
0.0093 0.4352 0.6442 0.8223 0.0669 0.0750 0.1579 0.4659 0.4245 0.6393 0.5641 0.4505 0.4841 0.5146 0.3356 0.3269 0.7271 0.9070 0.5396 0.0868 0.7764 0.3069 0.7120 0.8052 0.9633 0.6658 0.3569 0.1724 0.1989 0.1068 0.5921 0.7917 0.6307 0.8559 0.3566 0.4708 0.3033 0.5932 0.8028 0.4506 0.3292 0.9448 0.2092 0.5576 0.1514 0.5201 0.8899 0.5369 0.4797 0.8613 0.6280 0.1493 0.3335 0.0613 0.5330 0.3729 0.5056 0.2013 0.3167 0.1885 0.0947 0.8074 0.7094 0.3982 0.4959 0.4298 0.9395 0.8940 0.6521 0.5169 0.6788 0.1383 0.9413 0.5478 0.0879 0.6249 0.1888 0.8520 0.4720 0.5320 0.0991 0.5874 0.9938 0.3726 0.8688 0.4819 0.6446 0.2709 0.1690 0.0696 0.7237 0.6625 0.5572 0.2795 0.1393 0.5328 0.8897 0.1949 0.1757 0.2561 0.0574 0.4576 0.8966 0.7007 0.4071 0.4533 0.5061 0.2979 0.8647 0.7720 0.0163 0.8398 0.7416 0.2130 0.3288 0.0865 0.1613 0.2869 0.6096 0.1663 0.8057 0.2727 0.5270 0.3867 0.4939 0.0631 0.1537 0.0135 0.6568 0.6885 0.1418 0.0855 0.8256 0.8523 0.3056 0.7419 0.5254 0.7348 0.0874 0.6650 0.5307 0.6938 0.7397 0.1528 0.8003 0.1851 0.3351 0.1735 0.0691 0.5860
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
output(1:3)
ans = 1×3
0.0093 0.5921 0.0947
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Sum1=sum(output(1:3))
Sum1 = 0.6961
Thus you try to plot one single value (which seems to be 0 in your case).

Connectez-vous pour commenter.

Réponse acceptée

Mathieu NOE
Mathieu NOE le 2 Avr 2024
hello again
seems to me you want to sum the first 3 rows so
it should be :
Sum1=sum(output(1:3,:)); instead of Sum1=sum(output(1:3));
output = rand(5,2000)
output = 5x2000
0.1791 0.8425 0.6411 0.9862 0.3772 0.5695 0.0062 0.4191 0.0342 0.4738 0.1301 0.5518 0.6119 0.1510 0.9088 0.5260 0.4196 0.2989 0.1783 0.3134 0.7180 0.3725 0.1104 0.1097 0.2979 0.1851 0.7062 0.0656 0.1199 0.9486 0.5756 0.5375 0.3127 0.3970 0.1694 0.3851 0.8991 0.5512 0.2973 0.0736 0.0879 0.9771 0.1746 0.9450 0.8210 0.4277 0.6274 0.3267 0.7170 0.9286 0.1474 0.6039 0.4600 0.6433 0.4516 0.5993 0.9955 0.5155 0.3603 0.3852 0.1793 0.2607 0.9741 0.4219 0.0558 0.7762 0.7187 0.9905 0.8076 0.6963 0.0663 0.6098 0.1652 0.8677 0.4870 0.6077 0.4855 0.3860 0.0821 0.3845 0.3874 0.3454 0.6813 0.9306 0.7205 0.9261 0.8512 0.6857 0.4511 0.6153 0.8288 0.7844 0.1568 0.0911 0.2925 0.2977 0.1513 0.5298 0.4435 0.7714 0.5839 0.6496 0.3966 0.9823 0.9219 0.1772 0.1597 0.4256 0.9880 0.2713 0.7444 0.5459 0.4914 0.8019 0.6221 0.8412 0.1941 0.7266 0.0968 0.4999 0.9924 0.3889 0.5036 0.9485 0.9013 0.6529 0.4048 0.1062 0.2379 0.2751 0.9468 0.3906 0.3642 0.5598 0.9254 0.3007 0.4848 0.8409 0.0269 0.0066 0.3663 0.7956 0.3960 0.0903 0.1309 0.7850 0.9601 0.3460 0.0059 0.7290
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Sum1=sum(output(1:3,:))
Sum1 = 1x2000
0.9340 1.6407 1.9280 1.8050 0.6024 1.7308 1.6240 1.9609 1.1390 1.2437 0.2843 2.1387 0.9517 1.9636 2.2169 1.5615 1.5326 1.0116 0.9774 1.6264 1.2528 1.3218 1.2517 1.6836 1.4700 1.7104 2.5530 1.2668 0.9312 1.9491
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
plot(Sum1)
  3 commentaires
Stephen23
Stephen23 le 9 Avr 2024
"So this works great until I use any number above 5 as start or finish then I get: Index in position 1 exceeds array bounds. Index must not exceed 5."
Of course you will get that error: if you matrix only has five rows (as with the example give by Mathieu NOE) then what do you expect MATLAB to do when you try to access a non-existent sixth row? Or for that matter, any other row that does not exist.
S
S le 11 Avr 2024
@Stephen23 there are 32 rows

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by