# How to write a specific number via a loop

1 view (last 30 days)
Ivan Mich on 3 Sep 2021
Answered: Oliveira Pereira on 10 Sep 2021
I have a code that
2)calculates distances between points of file1 and point of file 2
3) writes an output with the distnces less than 2km.
The problem is that I would like to add to the output file one extra column that concludes specific number of each row
my code is
lat1=cell2mat(data(4:end,2))
lon1=cell2mat(data(4:end,3))
value=cell2mat(data(4:end,5))
filename1= 'file2.txt' %arxeio me makroseismika
[d1,tex]= importdata(filename1);
lat2=d1.data(:,2);
lon2=d1.data(:,1);
c=d1.data(:,4);
for z=1:numel(stname)
distances = (deg2km(distance(lat1(z),lon1(z),lat2,lon2)));% Compute distances
withinkm = distances <= 2; % Find row indexes where the distance is less than 2 km.
% Extract only those close points from the set of all points.
closePointskm = [lat2(withinkm), lon2(withinkm), c(withinkm), distances(withinkm), value(z:numel(withinkm))]
writematrix()
end
The problem is in the line 23.
command window shows me:

dpb on 3 Sep 2021
Other than there aren't 23 lines in the code shown :), the problem is actually in the line
distances = (deg2km(distance(lat1(z),lon1(z),lat2,lon2)));% Compute distances
because you save only the current value you just computed each time through the loop so you don't have but the last value at the end. Then, the same problem occurs with the next line.
Something like
...
distances=zeros(numel(stname),1); % preallocate the output array
for z=1:numel(stname)
distances(z)=deg2km(distance(lat1(z),lon1(z),lat2,lon2));
end
withinkm = distances <= 2; % Find row indexes where the distance is less than 2 km.
% Extract only those close points from the set of all points.
closePointskm = [lat2(withinkm), lon2(withinkm), c(withinkm), distances(withinkm), find(withinkm)];
...
This writes the row in the array for which the respective values was found/saved.
You'll then also need to fix up the syntax for the call to writematrix
dpb on 7 Sep 2021
"I want to "repeat" the same number for all tha output files"
That's the first time you've said that; it appeared you were looking to append the location in the file at which the distance search was found to be true from the rest of the code.
For the above, if you know what value "value" is to be (there's no hint regarding that in the above code other than it is an array the same size as the rest)
If you know which particular index into the array you're looking for, repmat() will likely be of interest. Otherwise, if it is the values from the array, it would seem the same logical addressing vector would be the logical (so to speak :) ) thing to use to pull the array values corresponding to everything else.

Oliveira Pereira on 10 Sep 2021
I worked on the old principle
Getting the sum using a for loop implies that you should:
Create an array of numbers, in the example int values.
Create a for statement, with an int variable from 0 up to the length of the array, incremented by one each time in the loop.
In the for statement add each of the array's elements to an int sum.