Calling matrices from workspace to For loop in MATLAB

In summary, to call a matrix from the workspace to a for loop in MATLAB, you can use the load function to load the matrix into the current workspace and then use the matrix name to access its values in the for loop. Additionally, you can use a for loop to iterate through each element of a matrix, modifying the elements by using the index within the for loop. It is also possible to use a for loop to perform operations on multiple matrices and create a new matrix from existing matrices.
  • #1
omri3012
62
0
Hallo,

I have matrices on my workpace and i need to take the first element from each one
and produce new vector. In other words, i have the matrices D1,D2,D3...D36 on my workspace and by using a For loop i want to generate a new vector:
d=[D1(1,1) D2(1,1)...D3(1,1)].
does anyone know how to that in For loop?

Thanks,
Omri
 
Physics news on Phys.org
  • #2
How about this:
Code:
for i = 1:36
    s = sprintf('d(i) = D%g(1,1);',i);
    eval(s);
end
 
  • #3
Thanks,
It's working now, i really appreciate it!

Omri
 
Last edited:

1. How do I call a matrix from the workspace to a for loop in MATLAB?

To call a matrix from the workspace to a for loop in MATLAB, you can use the load function to load the matrix into the current workspace. Then, you can use the name of the matrix to access its values within the for loop.

2. Can I use a for loop to iterate through each element of a matrix in MATLAB?

Yes, you can use a for loop to iterate through each element of a matrix in MATLAB. You can use the size function to determine the dimensions of the matrix and then use nested for loops to access each element.

3. How can I modify the elements of a matrix within a for loop in MATLAB?

To modify the elements of a matrix within a for loop in MATLAB, you can use the index of the element within the for loop. For example, if you want to add 1 to each element of a matrix, you can use matrix(i,j) = matrix(i,j) + 1 within the for loop.

4. Is it possible to use a for loop to perform operations on multiple matrices in MATLAB?

Yes, you can use a for loop to perform operations on multiple matrices in MATLAB. You can use the load function to load each matrix into the workspace and then use nested for loops to access and manipulate the elements of each matrix.

5. Can I use a for loop to create a new matrix from existing matrices in MATLAB?

Yes, you can use a for loop to create a new matrix from existing matrices in MATLAB. Within the for loop, you can use matrix operations such as addition, subtraction, or multiplication to combine the elements of the existing matrices into a new matrix.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
507
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top