Create a new .dat file by editing an existing one

Hi,
Iv'e got a .dat file and I would like to edit it by removing some elements in the mid of the columns and adding zeros in the beggining and end of it. I used the folowing lines to enter the exisitng .dat file and then create a new one
data_in = dlmread('old_data.dat');
data_out = [data_in(:,1), data_in(:,2), data_in(:,3)];
save new_data.dat data_out -ASCII
How can we remove elements from some points of the 'old_data.dat' and add zeros in some other, so that the 'new_data.dat ' will have the same size with the first/exisitng one?
Any help would be greatly appreciated.

댓글 수: 8

data_out = [data_in(:,1), data_in(:,2), data_in(:,3)];
may be replaced by
data_out = data_in(:,1:3); % less typing
"remove elements from some points ... and add zeros in some other" Yes, that can be done. How to do it depends on what shall be removed and added. Do you have variables containing indicies?
Vicky
Vicky 2019년 11월 25일
편집: per isakson 2019년 11월 25일
Thank you. I wrote them seperately as I'm planning to do different editing in each column. No, I don't have indicies. The dat file (part of it) is in the following form
.....
0.1266389 0.4864586 0.0000000
0.1096567 0.4969406 0.0000000
0.0120945 0.8265600 0.0000028
0.0114024 0.8087909 0.0000012
0.0117482 0.8179427 0.0000018
0.0120945 0.8265600 0.0000028
0.1266389 0.4864586 0.0000000
0.1096567 0.4969406 0.0000000
....
and I want for instance to rewrite the 3rd column in the following form
.....
0.0114024 0.8087909 0.0000000
0.1266389 0.4864586 0.0000000
0.1096567 0.4969406 0.0000000
0.0120945 0.8265600 0.0000028
0.0120945 0.8265600 0.0000028
0.1266389 0.4864586 0.0000000
0.1096567 0.4969406 0.0000000
0.0117482 0.8179427 0.0000000
....
per isakson
per isakson 2019년 11월 25일
편집: per isakson 2019년 11월 25일
I'm missing something and I don't like guessing based on examples. That's a task for Neural Networks:) Anyhow here is what I done.
%%
A = load( 'cssm_in.txt' );
R = load( 'cssm_out.txt' );
%%
[isA,locR] = ismembertol( A, R, 1e-9, 'ByRows',true );
[isR,locA] = ismembertol( R(:,1:2), A(:,1:2), 1e-9, 'ByRows',true );
%%
% B = A( isA, : );
%%
B = A( locA, : );
B(1,end) = 0;
B(end,end) = 0;
result
>> B==R
ans =
8×3 logical array
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
Thus B is equal to R.
Where cssm_in.txt and cssm_out.txt contains your data.
Your turn!
Vicky
Vicky 2019년 11월 25일
I'm sorry, but I can't follow your steps. Are you loading also the data file that we want to create? Maybe I'm missing things, but my problem is how we can modify the 3rd column of the first 8x3 matrix to the second 8x3 matrix above.
>> M = randi( 9, 8, 3 )
M =
9 9 4
9 6 6
5 1 2
8 8 7
2 9 1
4 7 3
9 7 1
8 7 1
>> M(:,3) = 0
M =
9 9 0
9 6 0
5 1 0
8 8 0
2 9 0
4 7 0
9 7 0
8 7 0
>>
Over and out
Ok, so given your example can you modify M so that you eventulay get the following (i.e. I removed two values of 3rd column, 7 and 1, and added 2 zeros instead), instead of making an entire column zero?
M =
9 9 0
9 6 6
5 1 2
8 8 2
2 9 3
4 7 3
9 7 1
8 7 0
I cannot understand where you found the values
[6,2,2,3,3,1]
that you have assigned to the third column.
And "two values of 3rd column, 7 and 1, and added 2 zeros instead" is hard to interpret given your required matrix.
Anyhow "given your example can you modify M so that you eventulay get the following"
%% My example
M = [
9 9 4
9 6 6
5 1 2
8 8 7
2 9 1
4 7 3
9 7 1
8 7 1 ];
%%
M(2:7,3) = reshape( [6,2,2,3,3,1], [],1 );
M([1,8],3) = 0;
disp(M)
displays
9 9 0
9 6 6
5 1 2
8 8 2
2 9 3
4 7 3
9 7 1
8 7 0
which is identical to your required matrix of the previous comment. (In your previous example you shuffled the rows in a way that I failed to understand.)
Before trying to make useful code you need to
Vicky
Vicky 2019년 11월 26일
Thank you for the help and the suggestions.

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

2019년 11월 25일

댓글:

2019년 11월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by