필터 지우기
필터 지우기

create a matrix from a text file

조회 수: 9 (최근 30일)
Hamid Hojaji
Hamid Hojaji 2020년 12월 9일
댓글: Hamid Hojaji 2020년 12월 9일
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

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 12월 9일
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);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by