Matlab - I want to create row matrix

  • Context: MATLAB 
  • Thread starter Thread starter thailovejj
  • Start date Start date
  • Tags Tags
    Matlab Matrix Row
Click For Summary
SUMMARY

The discussion focuses on creating a row matrix A from multiple row matrices B_1 to B_7 in MATLAB. The user initially employs the eval function to dynamically create variables, which is discouraged due to its complexity. Two alternative methods are proposed: using a cell array to store the B matrices or directly concatenating the rows from the loaded data into matrix A. Both alternatives enhance code clarity and maintainability.

PREREQUISITES
  • Basic understanding of MATLAB syntax and functions
  • Familiarity with matrix operations in MATLAB
  • Knowledge of cell arrays in MATLAB
  • Experience with file I/O operations in MATLAB
NEXT STEPS
  • Explore MATLAB cell arrays and their applications
  • Learn about efficient matrix concatenation techniques in MATLAB
  • Investigate best practices for avoiding the use of eval in MATLAB
  • Understand MATLAB file I/O functions for loading data
USEFUL FOR

This discussion is beneficial for MATLAB users, particularly those involved in data manipulation and matrix operations, as well as programmers seeking to improve code efficiency and readability.

thailovejj
Messages
1
Reaction score
0
I want to create row matrix A by using row matrix B_1 , B_2 , B_3 ,...,B_7

Example

B_1 = [1 2 3];
B_2 = [6 9 3];
B_3 = [4 7 5];
.
.
.
B_7 = [9 6 3];

Then A = [1 2 3 6 9 3 4 7 5 ... 9 6 3];

My way is

mea = input('Insert MEA No. :');

y = load(sprintf('%d.txt',mea),'-ascii');

for i=1:7
eval(['B_' num2str(i) '=y(i,:)']);
end

above code is create matrix B_1 - B_7

and Then

A = [B_1 B_2 B_3 ... B_7];

but I want the code which can use for other case.

Thank you
 
Last edited:
Physics news on Phys.org
If you start using the eval statement, you'll forever be stuck using it later on which makes code very difficult to understand.

Two alternatives would be to create a cell array:

A = [];
for i=1:7
B{i}=y(i,:);
A = [A B{i}]
end

or, if the B vectors are all the same length, then why use B at all?:

A = [];
for i=1:7
A = [A y(i,:)];
end
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
5K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
1
Views
3K
Replies
0
Views
2K