Matlab, how read and extract matrices

In summary, the individual is seeking assistance with extracting matrices and elements for their thesis using a for-loop in MATLAB. They are struggling to correctly write the for-loop and are unsure how to use the index in the loop as a string in the name of the matrices. They also mention extracting the second column of each matrix. However, important details such as the source of the matrices and what specific assistance they need remain unknown.
  • #1
Valentina*
1
0
Hello,
I'm new here and in matlab, so if I do mistakes, please, tell me.
I'm working on my thesis and I need to extract some matrices and elements, but I can't write the for-loop exactly. For example, I have to read six matrices (but then I'll have more matrices) and this is too long

matrix_1 = dlmread('/scratch/matrix_1');
matrix_2 = dlmread('/scratch/matrix_2');
matrix_3 = dlmread('/scratch/matrix_3');
...
...
and so on. I'd like to do a for loop, to read the matrices, but I don't know how. I tried with something like

number = [1, 2, 3, 4, 5, 6];
for ii = 1 : 6
tmp = dlmread(sprintf('/scratch/matrix_$i', number[ii]));
eval(sprintf('Matrix_final_$i = tmp', ii));
end

but it doesn't work :( In the same way, after, I have to extract the second column of each matrix, because is too long to do

n1 = matrix_1(:,2);
n2 = matrix_2(:,2);
...
...

I don't understand how to use the index ii in the for loop like a string in the name of the matrices. Does somebody know if it's possible?

Sorry for my English,
thank you!
 
Physics news on Phys.org
  • #2
Valentina* said:
I have to read six matrices
Read from where?

Some important questions remain unanswered, so it's not possible to post a good answer.
 

1. How do I read a matrix in Matlab?

To read a matrix in Matlab, you can use the load function. This function takes in the name of the file containing the matrix as its argument and returns the matrix as a variable.

2. Can I read multiple matrices from one file in Matlab?

Yes, you can read multiple matrices from one file in Matlab using the load function. This function will return a structure containing all the matrices in the file, which you can access using dot notation.

3. How do I extract a specific element from a matrix in Matlab?

To extract a specific element from a matrix in Matlab, you can use indexing. For example, if you want to extract the 3rd element from the 2nd row of a matrix A, you can use A(2,3). This will return the value at the specified element.

4. How do I extract a row or column from a matrix in Matlab?

To extract a row or column from a matrix in Matlab, you can use indexing as well. For example, if you want to extract the 2nd column of a matrix A, you can use A(:,2). This will return all the elements in the 2nd column as a column vector.

5. Can I extract a submatrix from a larger matrix in Matlab?

Yes, you can extract a submatrix from a larger matrix in Matlab using indexing. For example, if you want to extract the top left 3x3 submatrix from a 5x5 matrix A, you can use A(1:3,1:3). This will return the submatrix as a separate matrix.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
752
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top