How to write a specific number via a loop

조회 수: 2 (최근 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?

채택된 답변

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
Ivan Mich
Ivan Mich 2021년 9월 6일
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 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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

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.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by