Can MATLAB increment multiple rows of array data?

4 visualizzazioni (ultimi 30 giorni)
Can MATLAB increment multiple rows of array data?
Guys, got an interesting problem pertaining to incremenation. I have a space delimited text file contain several rows of data that look like this:
33000.000 33000.060 -5277.000000 1124.000000 -4138.000000 -3.000000 -5.000000
33001.000 33001.060 -5278.000000 1125.000000 -4139.000000 -4.000000 -6.000000
33002.000 33002.060 -5279.000000 1126.000000 -4140.000000 -5.000000 -7.000000
For processing purposes, I need to increment each data element by an equal value (in each row) several times and save the resulting data to a file. For example, if I want to increment each row equally by .2, the output would be:
33000.000 33000.060 -5277.000000 1124.000000 -4138.000000 -3.000000 -5.000000
33000.200 33000.260 -5277.200000 1124.200000 -4138.200000 -3.200000 -5.200000
33000.400 33000.460 -5277.400000 1124.400000 -4138.400000 -3.400000 -5.400000
33000.600 33000.660 -5277.600000 1124.600000 -4138.600000 -3.600000 -5.600000
33000.800 33000.860 -5277.800000 1124.800000 -4138.800000 -3.800000 -5.800000
33001.000 33001.060 -5278.000000 1125.000000 -4139.000000 -4.000000 -6.000000
33001.200 33001.260 -5278.200000 1125.200000 -4139.200000 -4.200000 -6.200000
33001.400 33001.460 -5278.400000 1125.400000 -4139.400000 -4.400000 -6.400000
33001.600 33001.660 -5278.600000 1125.600000 -4139.600000 -4.600000 -6.600000
33001.800 33001.860 -5278.800000 1125.800000 -4139.800000 -4.800000 -6.800000
33002.000 33002.060 -5279.000000 1126.000000 -4140.000000 -5.000000 -7.000000
33002.200 33002.260 -5279.200000 1126.200000 -4140.200000 -5.200000 -7.200000
33002.400 33002.460 -5279.400000 1126.400000 -4140.400000 -5.400000 -7.400000
33002.600 33002.660 -5279.600000 1126.600000 -4140.600000 -5.600000 -7.600000
33002.800 33002.860 -5279.800000 1126.800000 -4140.800000 -5.800000 -7.800000
33003.000 33003.060 -5280.000000 1127.000000 -4141.000000 -6.000000 -8.000000
I’ve searched for an existing MATLAB function that will accomplish this and found nothing.
Can something like this be done in MATLAB? If so, any ideas?

Risposta accettata

Brad
Brad il 12 Apr 2013
PT, I had to make a couple minor modifications. But this runs like a champ! Thank you.

Più risposte (1)

PT
PT il 11 Apr 2013
%%Load
filename = 'test.txt';
fh = fopen(filename, 'r');
Table = [];
Rows = {};
ncol = [];
j = 0;
while ~feof(fh)
line = fgetl(fh);
tokens = regexp(line, '(-?\d+(?:\.\d+)?)', 'tokens');
j = j + 1;
ncol(j,1) = length(b);
row = nan(1,ncol(j));
for i = 1:ncol
row(i) = str2double(tokens{i});
end
Rows{j,1} = row;
end
fclose(fh);
ncol_max = max(ncol);
Table = nan(j, ncol_max);
for i = 1:length(Rows)
Table(i, 1:ncol(i)) = Rows{i};
end
%%Process
IncValues = [0 0.2 0.4 0.6 0.8];
IncValues = IncValues(:);
[nrow, ncol] = size(Table);
NewTable = cell(nrow,1);
for i = 1:size(Table,1)
NewTable{i,1} = IncValues*ones(1,ncol) + ones(size(IncValues,1),1) * Table(i,:);
end
NewTable = cat(1,NewTable{:});
%%Save
newfilename = 'testout.txt';
fh = fopen(newfilename, 'w');
for i = 1:size(NewTable,1)
fprintf(fh, '%s\n', num2str(NewTable(i,:),'%f\t'));
end
fclose(fh);
  1 Commento
Brad
Brad il 21 Mag 2013
PT, this works great. I honked up the accepted answer button and somehow accepted an answer by me that doesn't exist.

Accedi per commentare.

Categorie

Scopri di più su Live Scripts and Functions in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by