Effacer les filtres
Effacer les filtres

create a matrix from a text file

9 vues (au cours des 30 derniers jours)
Hamid Hojaji
Hamid Hojaji le 9 Déc 2020
Commenté : Hamid Hojaji le 9 Déc 2020
Hi,
I want to create a matrix from a text file,the text file have 3 columns of numbers,first is row number,second is column number and the third column is values.
My program must first read a address of values in first and second column and afterthat put the value from third column in matrix.
What do you suggest?
thanks
  1 commentaire
Hamid Hojaji
Hamid Hojaji le 9 Déc 2020
It works,thanks a lot.

Connectez-vous pour commenter.

Réponse acceptée

Ameer Hamza
Ameer Hamza le 9 Déc 2020
Try sub2ind(): https://www.mathworks.com/help/matlab/ref/sub2ind.html to convert from row and column number to a linear index. Something like this should work
M = readmatrix('filename.txt');
rows = M(:,1);
cols = M(:,2);
data = zeros(max(rows), max(cols));
ind = sub2ind(size(data), rows, cols);
data(ind) = M(:,3);

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by