I have a matrix read from a text file and I want to subtract more matrix, delimited by the repeating line: 9999 0 0 0 0 0. I want to have as a result 5 different matrix, in this case. How can I do? thank you

501 1.547 0 0 0 0
500 73.164 100.4764 333.3890 1.500 00
502 58.793 99.6720 160.0586 1.500 00
9999 0 0 0 0 0
502 1.560 0 0 0 0
501 58.790 100.4551 60.7974 1.500 00
503 37.525 99.3758 200.0799 1.500 00
9999 0 0 0 0 0
503 1.592 0 0 0 0
502 37.524 100.8832 197.9390 1.500 00
504 69.050 99.8927 277.4128 1.500 00
9999 0 0 0 0 0
504 1.630 0 0 0 0
503 69.060 100.2973 130.6244 1.500 00
501 52.676 101.3383 229.5360 1.500 00
9999 0 0 0 0 0
501 1.547 0 0 0 0
504 52.675 98.8623 77.7161 1.500 00
500 73.164 100.4764 333.3890 1.500 00
As result for example, the first one should be
501 1.547 0 0 0 0
500 73.164 100.4764 333.3890 1.500 00
502 58.793 99.6720 160.0586 1.500 00
the second one
502 1.560 0 0 0 0
501 58.790 100.4551 60.7974 1.500 00
503 37.525 99.3758 200.0799 1.500 00
..........the last one
501 1.547 0 0 0 0
504 52.675 98.8623 77.7161 1.500 00
500 73.164 100.4764 333.3890 1.500 00

5 Comments

The shown result is not a matrix, because it is not rectangular. So please explain exactly, what you want to achieve.
Your "result" is a 1 by 2 row vector followed by a 2 by 6 matrix. Exactly where is the 5 by 5 matrix in the result, and exactly what was subtracted from what to get your result? Like if you did the subtraction C = A - B, what were A and B? And what was C, since it's not your result and we don't see any 5 by 5 matrix in sight?
And you say "depending on the repeating line" but that is not a condition - it's just simply a repeating line and not a true or false condition. So what depends on what? A condition needs to be true or false. For example you might say "depending on if 9999 is in the line, or not in the line"
Sorry, I'm new to Matlab The text file is read with dImread, so I have a matrix of 13 lines and 6 columns, with zeros completing the vector line. I have modified the question.
@Loghin Ana-Maria: Can you please upload the actual file? This makes us helping you a lot easier.

Sign in to comment.

 Accepted Answer

EDIT: to use the newly uploaded data file.
Probably the easiest way to split this would be to do this after reading the file into MATLAB:
% Read the file as text:
fid = fopen('drumuire.txt','rt');
C = cell2mat(textscan(fid,'%f%f%f%f%f%f'));
fclose(fid);
% identify groups between 9999's:
idx = [1,diff(C(:,1)'~=9999)];
idy = find(idx<0)-find(idx>0);
idy(2,:) = 1;
% Split data into a cell array:
D = mat2cell(C,idy(:),size(C,2));
D(2:2:end) = []; % optional...
This code automatically adjusts to the number of rows (i.e. must not be groups of three), but assumes that the rows with 9999 only occur individually (i.e. are not grouped together, two or more rows), and that the first row is not a 9999 row but that the last row is. This code returns all of the data in a cell array:
>> D
D =
[3x6 double]
[3x6 double]
[3x6 double]
[3x6 double]
[3x6 double]
>> D{1}
ans =
501.0000 1.5470 NaN NaN NaN NaN
500.0000 73.1640 100.4764 333.3890 1.5000 0
502.0000 58.7930 99.6720 160.0586 1.5000 0

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!