03 Apr 2014 08:25 AM |
|
Hello,
I have a very simple (I think) problem that I can't seem to figure out. I have an array with numbers (lake depths) and want to calculate the difference from each element to all the others and output that ideally as a table or at least as lines in the console. I created the following program with a nested loop that does the correct calculations, but it outputs the results as columns, and I want them one element at the time with the differences output horizontally (e.g. in one line).I want the output as something like this:
Lake1 0 7 3 0 7 -4
Lake2 -7 0 -4 -7 0 -11
Lake3 -3 4 0 -3 4 -7
Lake4 0 7 3 0 7 -4
Lake5 -7 0 -4 -7 0 -11
Lake6 4 11 7 4 11 0
Any help is appreciated. Thanks!
pro lakedepth
; This little program calculates lake depth differences from 6 different lakes to on another
; Input is an array with lake depths
; Output is printed of the differences
;
; Defining the array - this can be substituted as needed, or ideally re-written and drawn from an input file
; input depths are in meters
depth =[10,3,7,10,3,14]
; Calculating the differences, using a nested 'for loop' and printing it to the console
for i=0, 5 do begin
print, "Lake" + string(i+1)
for j=0, 5 do begin
lakediff = [depth[i]-depth[j]]
print, lakediff
endfor
endfor
|
|
|
|
Deleted User New Member
Posts:  
03 Apr 2014 10:16 AM |
|
Hi Mike,
What about this:
depth =[10,3,7,10,3,14]
; Calculating the differences, using a nested 'for loop' and printing it to the console
name =strarr(6)
lakediff=intarr(6)
for i=0, 5 do begin
name[i] = "Lake" + strtrim(string(i+1),1)
for j=0, 5 do begin
lakediff[j] = [depth[i]-depth[j]]
endfor
print, name[i], lakediff
endfor
will that work?
Cheers,
Fernando
|
|
|
|
Deleted User New Member
Posts:23  
03 Apr 2014 11:18 AM |
|
Hi Fernando,
Thanks so much for the quick reply and solution...that works. I need to wrap my head around this whole arrays and for loops a bit more!
Thanks again,
Mike
|
|
|
|
Deleted User New Member
Posts:  
|
Deleted User New Member
Posts:  
03 Apr 2014 01:23 PM |
|
Ah!, and also noticed that you don't really need my name[] array, because you could just print it in the same line you print the values:
depth =[10,3,7,10,3,14]
; Calculating the differences, using a nested 'for loop' and printing it to the console
lakediff=intarr(6)
for i=0, 5 do begin
for j=0, 5 do begin
lakediff[j] = [depth[i]-depth[j]]
endfor
print, "Lake" + strtrim(string(i+1),1), lakediff
endfor
end
Cheers!
fernando
|
|
|
|