How to replace columns in a datafile after performing mathematical operations to them?
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a datafile having 40 columns and have to divide first column with a constant number (let say 5) and rest 39 columns with a different number (i.e. 7). So, I want to replace all the columns in a new datafile. Can you suggest any short method for it.
I am trying the following code but its not replacing the column, and just appending a column according to last given mathematical operation (i.e. here appending the new calculated 3rd column to the first column).
x=5; % a constant
y=7; %another constant
a = textread('file1.txt');
a1 = a(:,1)/x;
a2 = a(:,2)/y;
a3 = a(:,3)/y; %third column
save ('New_file1.txt','-ascii')
type('New_file1.txt')
댓글 수: 0
채택된 답변
Voss
2022년 5월 28일
x=5; % a constant
y=7; %another constant
a = readmatrix('file1.txt');
disp(a)
new_a = a;
new_a(:,1) = new_a(:,1)/x;
new_a(:,2:end) = new_a(:,2:end)/y;
disp(new_a)
writematrix(new_a,'New_file1.txt')
type('New_file1.txt')
댓글 수: 6
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!