create account

29-01-2025 - Computer science basics - Matlab, cycles [EN]-[IT] by stefano.massari

View this thread on: hive.blogpeakd.comecency.com
· @stefano.massari ·
$12.05
29-01-2025 - Computer science basics - Matlab, cycles [EN]-[IT]
![image.png](https://files.peakd.com/file/peakd-hive/stefano.massari/23yU27YPngAmFXRBR5BGCwQCS1aDCiJThXd9o4TP7215LbchijaXc6yKJxVeu2xk1oZjK.png)


---

*~~~ La versione in italiano inizia subito dopo la versione in inglese ~~~*

---


**ENGLISH**
**29-01-2025 - Computer science basics - Matlab, cycles [EN]-[IT]**
With this post I would like to give a brief instruction about the topic mentioned in the subject
(code notes: X_54)

***Matlab, cycles***
In Matlab, a cycle is defined as the iterative structure that contains the section of code that must be repeated.
Loops in MATLAB are used to repeat a block of code multiple times, thus reducing the need to manually write repetitive instructions. This feature is useful in many situations, for example when you need to perform operations on large amounts of data, calculate numerical series or perform iterative simulations.
Some structures used for these cycles are widespread structures in most programming languages.

*The Matlab Iteration Structure*
The Matlab iteration structure has 5 fundamental characteristics:
1-It allows you to re-execute a part of the program multiple times based on a
counter and a termination condition
2-An iterative structure is used to repeat actions
3-The iterative structure contains the section of code that must be
repeated
4-A loop is an iterative control structure used to repeat
the execution of a set of instructions a certain number of times
5-Each repetition of the loop is called an iteration or step

There are operational structures in which the number of iterations is known a priori and others are not. For example, the *for* loop has a known number of iterations, while the *while* loop does not. In the *while* loop the number of iterations is not known a priori and the loop ends when a specific condition is satisfied

**Iterative and conditional constructs**
It is possible to nest for loops and conditional statements, paying attention to the fact that both must always be accompanied by the relative final end clause. These are the iterative and conditional constructs

Loops in Matlab are used to:
-Automate repetitive operations
-Work with vectors, matrices or arrays
-Solve iterative problems
-Dynamic conditions

Exercise:

Let's try an exercise on loops.
Below is the Matlab code that takes an array x as input and returns the element with the maximum value as output.

function massimo = trovaMassimo(x)
% FINDMASSIMO Returns the maximum element of an array.
% massimo = trovaMassimo(x) returns the maximum value of the array x.

% Check if array is empty
if isempty(x)
error('The input array is empty.')
end

% Calculate maximum value
maximum = max(x);
end

Here is an example of usage
x = [3, 7, 1, 9, 4];
maximumValue = findMax(x);
disp(['Maximum value is: ', num2str(maxValue)]);

*Code Description*
*01-First Block*
Checking the Array: The function checks if the array is empty. If it is, it throws an error.
*02-Second Block*
Calculating the Maximum: Uses the built-in MATLAB function max to find the maximum value of the array.
*03-Third Block*
Returning the Result: The maximum value is returned to the caller.

***Conclusions***
Matlab loops are essential for automating repetitive processes and working with large amounts of data.

***Question***
Have you ever written a program using "for" and "while" loops? Or have you used if, elseif, else loops?





---

https://images.hive.blog/1536x0/https://files.peakd.com/file/peakd-hive/green77/gGQutTRs-hive-spacer.png

---


**[ITALIAN]**
**29-01-2025 - Basi di informatica - Matlab, i cicli [EN]-[IT]**
Con questo post vorrei dare una breve istruzione a riguardo dell’argomento citato in oggetto
(code notes: X_54)

***Matlab, i cicli***
In Matlab viene definito ciclo la struttura iterativa che contiene la sezione di codice che deve essere ripetuta.
I cicli in MATLAB servono a ripetere un blocco di codice più volte, riducendo così la necessità di scrivere manualmente istruzioni ripetitive. Questa funzionalità è utile in molte situazioni, ad esempio quando si devono eseguire operazioni su grandi quantità di dati, calcolare serie numeriche o eseguire simulazioni iterative.
Alcune strutture usate per questi cicli sono strutture diffuse nella maggior parte dei linguaggi di programmazione.

*La struttura di iterazione di Matlab*
La struttura di iterazione di Matlab ha 5 caratteristiche fondamentali:
1-Consente di rieseguire una parte di programma più volte sulla base di un
contatore e di una condizione di termine
2-Una struttura iterativa viene usata per ripetere le azioni
3-La struttura iterativa contiene la sezione di codice che deve essere
ripetuta
4-Un ciclo è una struttura iterativa di controllo utilizzata per ripetere
l’esecuzione di un insieme di istruzioni un certo numero di volte
5-Ciascuna ripetizione del ciclo è detta iterazione o passo

Ci sono strutture operative in cui il numero di iterazioni è noto a priori e altre no. Ad esempio il ciclo *for* ha un numero di iterazioni noto, mentre il ciclo *while* no. Nel ciclo *while* il numero di iterazioni non è noto a priori e il ciclo termina quando viene soddisfatta una specifica condizione

**Costrutti iterativi e condizionali**
È possibile annidare i cicli for e le istruzioni condizionali prestando attenzione al fatto che entrambi devono essere sempre accompagnati dalla relativa clausola finale end. Questi sono i costrutti iterativi e condizionali

I cicli in Matlab si usano per:
-Automatizzare operazioni ripetitive
-Lavorare con vettori, matrici o array
-Risolvere problemi iterativi
-Condizioni dinamiche


Esercizio:

Proviamo a fare un esercizio sui cicli.
Qui di seguito è riportato il codice Matlab che prende in ingresso un array x e restituisce in uscita l’elemento con valore massimo.

function massimo = trovaMassimo(x)
    % TROVAMASSIMO Restituisce l'elemento massimo di un array.
    % massimo = trovaMassimo(x) restituisce il valore massimo dell'array x.

    % Controllo se l'array è vuoto
    if isempty(x)
        error('L''array in ingresso è vuoto.')
    end

    % Calcolo del valore massimo
    massimo = max(x);
end

Qui di seguito un esempio di utilizzo
x = [3, 7, 1, 9, 4];
valoreMassimo = trovaMassimo(x);
disp(['Il valore massimo è: ', num2str(valoreMassimo)]);

*Descrizione del codice*
*01-Primo blocco*
Controllo dell'array: La funzione verifica se l'array è vuoto. Se lo è, genera un errore.
*02-Secondo blocco*
Calcolo del massimo: Utilizza la funzione MATLAB incorporata max per trovare il valore massimo dell'array.
*03-Terzo blocco*
Restituzione del risultato: Il valore massimo viene restituito al chiamante.


***Conclusioni***
I cicli di Matlab sono fondamentali per automatizzare processi ripetitivi e lavorare con grandi quantità di dati.

***Domanda***
Avete mai scritto un programma usando i cicli "for" e "while"? Oppure avete usato i cicli if, elseif, else?


**THE END**
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 684 others
properties (23)
authorstefano.massari
permlink29-01-2025-computer-science-basics-matlab-cycles-en-it
categoryhive-146620
json_metadata{"app":"peakd/2025.1.3","format":"markdown","tags":["learn","computerscience","neoxian","stem","ctp","pimp","palnet","vyb","archon"],"users":[],"image":["https://files.peakd.com/file/peakd-hive/stefano.massari/23yU27YPngAmFXRBR5BGCwQCS1aDCiJThXd9o4TP7215LbchijaXc6yKJxVeu2xk1oZjK.png","https://images.hive.blog/1536x0/https://files.peakd.com/file/peakd-hive/green77/gGQutTRs-hive-spacer.png"]}
created2025-01-29 04:59:00
last_update2025-01-29 04:59:00
depth0
children13
last_payout2025-02-05 04:59:00
cashout_time1969-12-31 23:59:59
total_payout_value5.989 HBD
curator_payout_value6.056 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,878
author_reputation336,937,918,210,859
root_title"29-01-2025 - Computer science basics - Matlab, cycles [EN]-[IT]"
beneficiaries
0.
accountbalaenoptera
weight300
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id140,385,760
net_rshares29,328,615,330,257
author_curate_reward""
vote details (748)
@arc7icwolf ·
$0.07
>Avete mai scritto un programma usando i cicli "for" e "while"? Oppure avete usato i cicli if, elseif, else?

Sì, mi è capitato di usarli tutti quanti (meno forse elseif, che su python è elif): sono uno dei pochi strumenti che sono in grado di utilizzare in realtà 😅

Comunque non sapevo che sapessi programmare! Che linguaggi conosci? :)
👍  ,
properties (23)
authorarc7icwolf
permlinkre-stefanomassari-sqv4s1
categoryhive-146620
json_metadata{"tags":["hive-146620"],"app":"peakd/2025.1.3","image":[],"users":[]}
created2025-01-29 18:31:15
last_update2025-01-29 18:31:15
depth1
children4
last_payout2025-02-05 18:31:15
cashout_time1969-12-31 23:59:59
total_payout_value0.032 HBD
curator_payout_value0.033 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length338
author_reputation494,058,388,866,713
root_title"29-01-2025 - Computer science basics - Matlab, cycles [EN]-[IT]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id140,401,834
net_rshares157,616,600,852
author_curate_reward""
vote details (2)
@stefano.massari ·
Ciao arc7, in realtà io non sono un programmatore. Anche se in passato è vero, ho studiato diversi linguaggi di programmazione.però stiamo parlando di cose veramente primitive. Iniziai a fare qualche piccolo programma con il linguaggio turbo Pascal ed il linguaggio Basic. La mia esperienza con la programmazione è andata avanti ma utilizzando poi fogli elettronici, quindi direi che non possiamo più chiamarla programmazione. Mi dispiace averti illuso ma non sono un programmatore, oggi non posso essere definito così. Per quanto riguarda Matlab, l’ho usato sopratutto per fare calcoli matriciali complessi, ma solo per curiosità personale. Invece da quel che leggo, tu si che sei un programmatore. !ALIVE
properties (22)
authorstefano.massari
permlinkre-arc7icwolf-2025130t18455496z
categoryhive-146620
json_metadata{"type":"comment","tags":["hive-146620"],"app":"ecency/3.2.1-mobile","format":"markdown+html"}
created2025-01-30 17:45:54
last_update2025-01-30 17:45:54
depth2
children3
last_payout2025-02-06 17:45:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length706
author_reputation336,937,918,210,859
root_title"29-01-2025 - Computer science basics - Matlab, cycles [EN]-[IT]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id140,428,788
net_rshares0
@arc7icwolf ·
$0.05
Onestamente penso che tu ne sappia più di me! Da quel che leggo hai delle ottime basi, cosa che invece io non ho 😅 più avanti voglio poi rileggermi un po' di questi post su Matlab che hai pubblicato, perchè creare grafici per visualizzare meglio i dati è qualcosa che sicuramente mi interessa e vorrei imparare :)

!PIZZA
👍  ,
properties (23)
authorarc7icwolf
permlinkre-stefanomassari-sqz9g5
categoryhive-146620
json_metadata{"tags":["hive-146620"],"app":"peakd/2025.1.3","image":[],"users":[]}
created2025-02-01 00:02:30
last_update2025-02-01 00:02:30
depth3
children2
last_payout2025-02-08 00:02:30
cashout_time1969-12-31 23:59:59
total_payout_value0.024 HBD
curator_payout_value0.024 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length321
author_reputation494,058,388,866,713
root_title"29-01-2025 - Computer science basics - Matlab, cycles [EN]-[IT]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id140,464,461
net_rshares122,466,884,468
author_curate_reward""
vote details (2)
@bisolamih ·
$0.07
Matlab course keeps getting wider everyday…
It’s more interesting though 
👍  
properties (23)
authorbisolamih
permlinkre-stefanomassari-2025129t151229333z
categoryhive-146620
json_metadata{"type":"comment","tags":["hive-146620","learn","computerscience","neoxian","stem","ctp","pimp","palnet","vyb","archon"],"app":"ecency/3.2.1-mobile","format":"markdown+html"}
created2025-01-29 14:12:30
last_update2025-01-29 14:12:30
depth1
children2
last_payout2025-02-05 14:12:30
cashout_time1969-12-31 23:59:59
total_payout_value0.034 HBD
curator_payout_value0.033 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length73
author_reputation71,359,083,962,322
root_title"29-01-2025 - Computer science basics - Matlab, cycles [EN]-[IT]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id140,395,447
net_rshares160,557,286,269
author_curate_reward""
vote details (1)
@stefano.massari ·
thanks for stopping by and leaving a comment. Unfortunately I have to tell you that I will write more about Matlab, but now my knowledge of this topic is almost over. !LOLZ
properties (22)
authorstefano.massari
permlinkre-bisolamih-2025130t183916977z
categoryhive-146620
json_metadata{"type":"comment","tags":["hive-146620","learn","computerscience","neoxian","stem","ctp","pimp","palnet","vyb","archon"],"app":"ecency/3.2.1-mobile","format":"markdown+html"}
created2025-01-30 17:39:15
last_update2025-01-30 17:39:15
depth2
children1
last_payout2025-02-06 17:39:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length172
author_reputation336,937,918,210,859
root_title"29-01-2025 - Computer science basics - Matlab, cycles [EN]-[IT]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id140,428,580
net_rshares0
@lolzbot ·
<div class='pull-right'><center><img src="https://lolztoken.com/lolz.png"><br><a href="https://lolztoken.com">lolztoken.com</a></p><br><br><br><br></center></div><p><center><strong>I was fired from the keyboard factory yesterday.<br>I wasn't putting in enough shifts.</strong><br><sub>Credit: <a href="https://peakd.com/@reddit">reddit</a></sub><br>@bisolamih, I sent you an <a href="https://lolztoken.com">$LOLZ</a> on behalf of stefano.massari<br><br>(1/8)<br>Delegate Hive Tokens to Farm $LOLZ and earn 110% Rewards.  <a href='https://peakd.com/@lolztoken/introducing-lolz-defi-now-you'>Learn more.</a></center></p>
properties (22)
authorlolzbot
permlinkre-re-bisolamih-2025130t183916977z-20250130t173927z
categoryhive-146620
json_metadata"{"app": "beem/0.24.19"}"
created2025-01-30 17:39:33
last_update2025-01-30 17:39:33
depth3
children0
last_payout2025-02-06 17:39:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length618
author_reputation196,466,767,504,289
root_title"29-01-2025 - Computer science basics - Matlab, cycles [EN]-[IT]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id140,428,589
net_rshares0
@discovery-it ·
$0.06
<div class="pull-left">https://cdn.steemitimages.com/DQmTAn3c753LR7bHCLPo96g9UvRMaPFwaMYn8VQZa85xczC/discovery_logo_colore%20-%20Copia.png</div><br> This post was shared and voted inside the discord by the curators team of <a href="https://discord.gg/cMMp943"> discovery-it</a> <br>Join our <a href = "https://hive.blog/trending/hive-193212"> Community</a> and follow our <a href = "https://hive.vote/dash.php?i=1&trail=discovery-it">Curation Trail</a><br>Discovery-it is also a Witness, vote for us <a href = "https://hivesigner.com/sign/account-witness-vote?witness=discovery-it&approve=true"> here</a>  <br>Delegate to us for passive income. Check our <a href = "https://hive.blog/hive-193212/@discovery-it/delegations-program-80-fee-back"> 80% fee-back Program</a> <hr>
👍  
properties (23)
authordiscovery-it
permlinkre-stefano-massari-2y59qp1oji
categoryhive-146620
json_metadata"{"app": "beem/0.24.26"}"
created2025-01-30 02:30:27
last_update2025-01-30 02:30:27
depth1
children0
last_payout2025-02-06 02:30:27
cashout_time1969-12-31 23:59:59
total_payout_value0.031 HBD
curator_payout_value0.031 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length773
author_reputation66,794,403,885,869
root_title"29-01-2025 - Computer science basics - Matlab, cycles [EN]-[IT]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id140,411,201
net_rshares151,945,638,184
author_curate_reward""
vote details (1)
@libertycrypto27 ·
$0.06
!discovery 20
👍  
properties (23)
authorlibertycrypto27
permlinkre-stefanomassari-2025130t33013997z
categoryhive-146620
json_metadata{"tags":["learn","computerscience","neoxian","stem","ctp","pimp","palnet","vyb","archon"],"app":"ecency/4.0.2-vision","format":"markdown+html"}
created2025-01-30 02:30:15
last_update2025-01-30 02:30:15
depth1
children1
last_payout2025-02-06 02:30:15
cashout_time1969-12-31 23:59:59
total_payout_value0.032 HBD
curator_payout_value0.032 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length13
author_reputation1,946,150,323,990,792
root_title"29-01-2025 - Computer science basics - Matlab, cycles [EN]-[IT]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id140,411,198
net_rshares153,307,832,255
author_curate_reward""
vote details (1)
@stefano.massari ·
Grazie Liberty per il tuo costante supporto. Con questo articolo volevo mostrare anche che Matlab è sia un programma che un linguaggio di programmazione. Ho visto diverse discussioni su questo, c’è che dice che questo software sia un programma, c’è chi afferma che questo software invece sia un linguaggio di programmazione. Con tutti gli sviluppi degli ultimi 15 anni che questo software ha subito, in realtà oggi è veramente entrambe le cose. !DHEDGE
properties (22)
authorstefano.massari
permlinkre-libertycrypto27-2025130t184941400z
categoryhive-146620
json_metadata{"type":"comment","tags":["hive-146620","learn","computerscience","neoxian","stem","ctp","pimp","palnet","vyb","archon"],"app":"ecency/3.2.1-mobile","format":"markdown+html"}
created2025-01-30 17:49:42
last_update2025-01-30 17:49:42
depth2
children0
last_payout2025-02-06 17:49:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length452
author_reputation336,937,918,210,859
root_title"29-01-2025 - Computer science basics - Matlab, cycles [EN]-[IT]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id140,428,892
net_rshares0
@pizzabot ·
<center>PIZZA!


$PIZZA slices delivered:
@arc7icwolf<sub>(1/10)</sub> tipped @stefano.massari 


</center>
properties (22)
authorpizzabot
permlinkre-29-01-2025-computer-science-basics-matlab-cycles-en-it-20250201t000253z
categoryhive-146620
json_metadata"{"app": "pizzabot"}"
created2025-02-01 00:02:54
last_update2025-02-01 00:02:54
depth1
children0
last_payout2025-02-08 00:02:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length107
author_reputation7,429,613,346,379
root_title"29-01-2025 - Computer science basics - Matlab, cycles [EN]-[IT]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id140,464,469
net_rshares0
@successwrite ·
$0.16
How come I am just hearing about this for the first time in computer science 
👍  
properties (23)
authorsuccesswrite
permlinkre-stefanomassari-squn3d
categoryhive-146620
json_metadata{"tags":["hive-146620"],"app":"peakd/2025.1.3","image":[],"users":[]}
created2025-01-29 12:09:15
last_update2025-01-29 12:09:15
depth1
children0
last_payout2025-02-05 12:09:15
cashout_time1969-12-31 23:59:59
total_payout_value0.081 HBD
curator_payout_value0.082 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length77
author_reputation397,367,899
root_title"29-01-2025 - Computer science basics - Matlab, cycles [EN]-[IT]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id140,392,270
net_rshares388,694,208,055
author_curate_reward""
vote details (1)