MathWorks - Mobile View
  • Melden Sie sich bei Ihrem MathWorks Konto anMelden Sie sich bei Ihrem MathWorks Konto an
  • Access your MathWorks Account
    • Eigener Account
    • Mein Community Profil
    • Lizenz zuordnen
    • Abmelden
  • Produkte
  • Lösungen
  • Forschung und Lehre
  • Support
  • Community
  • Veranstaltungen
  • MATLAB erhalten
MathWorks
  • Produkte
  • Lösungen
  • Forschung und Lehre
  • Support
  • Community
  • Veranstaltungen
  • MATLAB erhalten
  • Melden Sie sich bei Ihrem MathWorks Konto anMelden Sie sich bei Ihrem MathWorks Konto an
  • Access your MathWorks Account
    • Eigener Account
    • Mein Community Profil
    • Lizenz zuordnen
    • Abmelden

Videos und Webinare

  • MathWorks
  • Videos
  • Videos Homepage
  • Suche
  • Videos Homepage
  • Suche
  • Vertrieb kontaktieren
  • Testsoftware
  Register to watch video
  • Description
  • Full Transcript
  • Related Resources

How to Store Data in a Matrix

Elsie Eigerman, MathWorks

Do you need a for loop to populate a matrix? In this video step through a few different ways to store data in a matrix in MATLAB® with and without for loops. 

In today's video on MATLAB basics, we're going to show how to store the results of a calculation inside of a vector, which is a special case of a matrix. What we're going to do is say for I is equal 1 : 10, meaning that we're going to count from 1 to 10. Now inside of this loop what we're going to do is say Y is equal to I plus rand. So we're going to just have a random number generated—somewhere between 0 and 1—and add it to the current value of I, and end.

So I want to actually see the results of this. So I'm going to take the semicolon off. Now let's run this. I'm going to run it by hitting F5, which means save and run the current file. And we can see we've gone through this loop 10 times and gotten different values of Y.

Now what if we wanted to plot those? Well, every time through this loop we have overwritten the value of Y so we lost, like for instance, 9.528 when we generated 10.857. That isn't going to do very well if we want to plot this data.

So what we can do is come in here and say I want to make Y into a vector. So every time through the loop now this statement is going to read Y element 1 or 2, or 3, or 4, is going to equal to the same thing it did before. Now let's run this again. I'm going to do that by hitting F5. And what we'll see by scrolling up through the Command Window here is that at first, we have Y is equal to a 1 by 1, then a 1 by 2, 1 by 3. And each time we keep adding another column to this. And so we're storing that data.

Now that it's done what we can do is come in here and say Plot (y), and we can see that on the graph here. So this is a very simple example of a technique that is used all the time in MATLAB where you will just take the results and store them in a matrix for easy manipulation and use later.

This would also work if you were calculating a vector each time through the loop and wanted to store it as another column. So each column might represent one time through your loop. If your calculation is creating a matrix each time, you would then use a three-dimensional matrix, and so on. MATLAB actually supports n-dimensional matrices, so you can see how this can work for multiple dimensions.

Thank you.

Related Products

  • MATLAB
Related Information
MATLAB Video Blog

Feedback

Featured Product

MATLAB

  • Request Trial
  • Get Pricing

Up Next:

2:09
How to Store a Series of Vectors from a for Loop

Related Videos:

4:38
How to Do a Matrix Reshape by 'blocks'
6:10
Simple Example of Storing Data as a Matrix or Structure
5:31
Find a Quote in MathWorks Store
3:50
How to Plot from a Matrix or Table

View more related videos

MathWorks - Domain Selector

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web site

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • 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)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
    • 简体中文Chinese
    • English
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

  • Vertrieb kontaktieren
  • Testsoftware

Produkte

  • MATLAB
  • Simulink
  • Software für Studierende
  • Hardware-Unterstützung
  • File Exchange

Testen oder Kaufen

  • Downloads
  • Testsoftware
  • Vertrieb kontaktieren
  • Preise und Lizenzierung
  • Store

Lernen

  • Dokumentation
  • Tutorials
  • Beispiele
  • Videos und Webinare
  • Schulungen

Support

  • Hilfe zur Installation
  • Antworten
  • Consulting
  • License Center
  • Support kontaktieren

Über MathWorks

  • Jobs & Karriere
  • Newsroom
  • Social Mission
  • Vertrieb kontaktieren
  • Über MathWorks

MathWorks

Accelerating the pace of engineering and science

MathWorks ist der führende Entwickler von Software für mathematische Berechnungen für Ingenieure und Wissenschaftler.

Entdecken Sie…

  • Select a Web Site United States
  • Patente
  • Handelsmarken
  • Datenschutz
  • Datendiebstahl verhindern
  • Status von Anwendungen

© 1994-2021 The MathWorks, Inc.

  • Facebook
  • Twitter
  • Instagram
  • YouTube
  • LinkedIn
  • RSS

Folgen Sie uns

This website uses cookies to improve your user experience, personalize content and ads, and analyze website traffic.  By continuing to use this website, you consent to our use of cookies.  Please see our Privacy Policy to learn more about cookies and how to change your settings.