Matlab - I want to create row matrix

In summary, the code discussed is used to create a row matrix A by combining multiple row matrices B_1 to B_7, where B_1 to B_7 are created from a text file using the MEA number as input. This can be achieved using a cell array or by directly combining the B vectors, depending on their length and usage.
  • #1
thailovejj
1
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
  • #2
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
 

Related to Matlab - I want to create row matrix

1. What is the syntax for creating a row matrix in Matlab?

In order to create a row matrix in Matlab, you can use the following syntax:
matrixName = [value1, value2, value3]

2. Can I create a row matrix with different data types in Matlab?

Yes, Matlab allows you to create a row matrix with different data types. For example, you can have a row matrix with both numeric and string values.

3. How can I add elements to a row matrix in Matlab?

To add elements to a row matrix in Matlab, you can use the "concatenation" operator, which is the square brackets [ ]. For example, if you have a row matrix named "A" with values [1, 2, 3], and you want to add the value 4 to the end, you can use the following syntax:
A = [A, 4]

4. Is there a limit to the number of elements in a row matrix in Matlab?

No, there is no limit to the number of elements in a row matrix in Matlab. However, it is important to consider memory limitations and the performance of your code when working with large matrices.

5. How can I access and manipulate specific elements in a row matrix in Matlab?

To access and manipulate specific elements in a row matrix in Matlab, you can use indexing. Indexing starts at 1, so the first element in a row matrix would be accessed with index 1. For example, to access the second element in a row matrix named "A", you can use the syntax:
A(2)
You can also use indexing to change the value of an element in a row matrix, such as A(2) = 5 to change the second element to the value 5.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
962
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
481
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top