Calling matrices from workspace to For loop in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter omri3012
  • Start date Start date
  • Tags Tags
    Loop Matlab Matrices
Click For Summary
SUMMARY

The discussion focuses on extracting the first element from multiple matrices in MATLAB using a for loop. The user, Omri, has matrices named D1 through D36 in their workspace and seeks to create a new vector containing the first element of each matrix. A solution was provided using the eval function within a for loop to dynamically reference each matrix. The final code snippet successfully generates the desired vector.

PREREQUISITES
  • Basic understanding of MATLAB syntax and functions
  • Familiarity with matrix indexing in MATLAB
  • Knowledge of for loops in programming
  • Experience with the eval function in MATLAB
NEXT STEPS
  • Explore MATLAB matrix manipulation techniques
  • Learn about the implications and alternatives to using eval in MATLAB
  • Research vectorization methods in MATLAB for performance optimization
  • Investigate MATLAB's built-in functions for handling multiple matrices
USEFUL FOR

MATLAB users, data analysts, and engineers looking to automate matrix operations and improve their coding efficiency in MATLAB.

omri3012
Messages
60
Reaction score
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
How about this:
Code:
for i = 1:36
    s = sprintf('d(i) = D%g(1,1);',i);
    eval(s);
end
 
Thanks,
It's working now, i really appreciate it!

Omri
 
Last edited:

Similar threads

Replies
3
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K