How to replace columns in a datafile after performing mathematical operations to them?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
Voss
am 28 Mai 2022
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!