MATLAB Answers

How to write a specific number via a loop

4 ビュー (過去 30 日間)
Ivan Mich
Ivan Mich 2021 年 9 月 3 日
回答済み: Oliveira Pereira 2021 年 9 月 10 日
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?

回答 (2 件)

dpb
dpb 2021 年 9 月 3 日
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 件のコメント
dpb
dpb 2021 年 9 月 7 日
"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
Oliveira Pereira 2021 年 9 月 10 日
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.

Community Treasure Hunt

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

Start Hunting!

Translated by