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')

채택된 답변

Voss
Voss 2022년 5월 28일
x=5; % a constant
y=7; %another constant
a = readmatrix('file1.txt');
disp(a)
1 2 3 4 5 6 7 8 9 10 11 12
new_a = a;
new_a(:,1) = new_a(:,1)/x;
new_a(:,2:end) = new_a(:,2:end)/y;
disp(new_a)
0.2000 0.2857 0.4286 0.5714 1.0000 0.8571 1.0000 1.1429 1.8000 1.4286 1.5714 1.7143
writematrix(new_a,'New_file1.txt')
type('New_file1.txt')
0.2,0.285714285714286,0.428571428571429,0.571428571428571 1,0.857142857142857,1,1.14285714285714 1.8,1.42857142857143,1.57142857142857,1.71428571428571
  댓글 수: 6

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by