How to write a specific number via a loop

2 Ansichten (letzte 30 Tage)
Ivan Mich
Ivan Mich am 3 Sep. 2021
Beantwortet: Oliveira Pereira am 10 Sep. 2021
I have a code that
1)reads two files
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
data=readcell('file1.txt')
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:
Could you please help me?

Akzeptierte Antwort

dpb
dpb am 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
  7 Kommentare
Ivan Mich
Ivan Mich am 6 Sep. 2021
I am using this command
closePointskm = [lat(withinkm), lon(withinkm), IMM(withinkm), distances(withinkm), value(z)]
but command windows shows me
Index in position 2 exceeds array bounds (must not exceed 1).
Error in code (line 23)
because one has more than one lines in 5th column. The problem is that I want to "repeat" the same number for all tha output files, no matter how many lines (rows) are included in output file.
Could anyone please help me?
dpb
dpb am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Oliveira Pereira
Oliveira Pereira am 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.

Kategorien

Mehr zu Matrix Indexing 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!

Translated by