print and save matrix in fortran - fortran

Hello everyone I am new to Fortran and I am facing a problem. Let s assume I have a matrix a(5,50)
a1 a2 a3 a4 a5 a6 a7 etc
b1 b2 b3 b4 b5 b6 b7 etc
c1 c2 c3 c4 c5 c6 c7 etc
d1 d2 d3 d4 d5 d6 d7 etc
e1 e2 e3 e4 e5 e6 e7 etc
is there a way to save it into a file and print the matrix like the following
a1 b1 c1 d1 e1
a2 b2 c2 d2 e2
a3 b3 c3 d3 e3
etc
without saving it to another matrix? Because ok i can always do a loop and save it to a new matrix and then save that to a file and print it. I have also created a subroutine to print my matrix in a correct order and be presentable

Sure.
You could loop over the first index, then write the whole column:
do ii = 1, 50
write(unit, '(5(I7))') a(ii, :)
end do
Or you could use transpose:
write(unit, '(5(I7))') transpose(a)
(I'm assuming that a is an integer array and that all values can be written with 6 or fewer digits (including sign). Change the format if that's not the case.)
This computer doesn't have a fortran compiler, so I haven't tested it, but it should work.
Cheers

Related

Pine script conditions / conditional-statements

I have 6 conditions:
c1 = ...
c2 = ...
Then if 4 of them are fullfilled (yielding 15 combinations), I will execute some command. How to do this?
E.g.:
cb1 = c1 and c2 and c3 and c4
cb2 = c1 and c2 and c3 and c5
cb3 = c1 and c2 and c3 and c6
cb4 = c1 and c2 and c4 and c5
cb5 = c1 and c2 and c4 and c6
cb6 = c1 and c2 and c5 and c6
cb7 = c1 and c3 and c4 and c5
cb8 = c1 and c3 and c4 and c6
cb9 = c1 and c3 and c5 and c6
cb10 = c1 and c4 and c5 and c6
cb11 = c2 and c3 and c4 and c5
cb12 = c2 and c3 and c4 and c6
cb13 = c2 and c3 and c5 and c6
cb14 = c2 and c4 and c5 and c6
cb15 = c3 and c4 and c5 and c6
// Set up alert
alertcondition(condition=cb1 or cb2 or cb3 or cb4 or cb5 or cb6 or cb7 or cb8 or cb9 or cb10 or cb11 or cb12 or cb13 or cb14 or cb15,
message="cb")
You can read this for some information.
if (condition1 == true) and (condition2 == true) and (condition3 == true) and (condition4 == true)
// Do something
else if (condition2 == true) and (condition3 == true) and (condition4 == true) and (condition5 == true)
// Do something else
Please note the indentation.
Could we do the point system? example
condition1 is ok X=1
condition2 is ok X=x+1
condition3 is ok X=x+3
condition4 is ok X=x+4
if x>0
do this
else if x>1
do this
else if x>2
do this
...

Loop interval changing unexpectedly

I am writing a loop to remove every third element from an array until there is only one element left.
here is the code...
int elimcnt = 1;//counts how many elements looped through
int cnt = 0;//counts how many elements deleted for printing purposes
for (int i = 0; v.size() > 1; i++, elimcnt++) {
if (i == v.size()) {//reset i to the beginning when it hits the end
i = 0;
}
if (elimcnt%in.M == 0 && elimcnt != 0) {//in.M is elimination index which is 3
v.erase(v.begin() + (elimcnt%v.size()) - 1);
cnt++;
if (cnt%in.K == 0) {//in.K is how often you will print which is after 7 deletes
print_vector(v, cnt);
}
}
}
what actually happens when i run it is that it will correctly delete the first element but after that it deletes every 4th element from there on out.
Here is an example input...
A1 A2 A3 A4 A5 A6 A7 A8 A9 B1 B2 B3
B4 B5 B6 B7 B8 B9 C1 C2 C3 C4 C5 C6
C7 C8 C9 D1 D2 D3 D4 D5 D6 D7 D8 D9
E1 E2 E3 E4 E5
What is supposed to be outputted...
A1 A2 A4 A5 A7 A8 B1 B2 B4 B5 B7 B8
C1 C2 C4 C5 C6 C7 C8 C9 D1 D2 D3 D4
D5 D6 D7 D8 D9 E1 E2 E3 E4 E5
This is what is actually outputted...
A1 A2 A4 A5 A6 A8 A9 B1 B3 B4 B5 B7
B8 B9 C2 C3 C4 C6 C7 C8 D1 D2 D3 D4
D5 D6 D7 D8 D9 E1 E2 E3 E4 E5
I cant seem to figure out what is causing the code to do this so any help will be greatly appreciated.
The problem is in the expression used in the statement
v.erase(v.begin() + (elimcnt%v.size()) - 1);
^^^^^^^^^^^^^^^^^^^^^
Consider a sequence of numbers
1, 2, 3, 4, 5, 6
For the first traversing of the sequence You need to delete 3 and 6
After deleting 3 you will get
1, 2, 4, 5, 6
and the variable elimcnt after the deleting will be incremented and will be equal to 4. However the size of the sequence is now equal to 5. So when elimcnt will be equal to 6 then the expression elimcnt%v.size()) - 1 will be equal to 0 and the element 1 will be deleted.
I could suggest a more safe approach using iterators.
for example
size_t elimcnt = 0;//counts how many elements looped through
size_t cnt = 0;
for (auto it = v.begin(); v.size() > 1; it == v.end() ? it = v.begin() : it )
{
if (++elimcnt % in.M == 0)
{
it = v.erase(it);
if (++cnt % in.K == 0)
{
print_vector(v, cnt);
}
}
else
{
++it;
}
}

C++ Matrix horizontal concat

I have 2 matrix, for example:
a1 a2 a3 a4 a5 a6 a7 a8
M1 = b1 b2 b3 b4 M2 = b5 b6 b7 b8
c1 c2 c3 c4 c5 c6 c7 c8
what i want is get a matrix concat like this:
a1 a2 a3 a4 a5 a6 a7 a8
Mr = b1 b2 b3 b4 b5 b6 b7 b8
c1 c2 c3 c4 c5 c6 c7 c8
fast as possible cause my program is all based on this concat at speed of 50MHz.(Sound acquisition)
It's actually neded for read a single line fast(each line is a microphone flow).
If you save your matrix as a std::vector<std::vector<double>>, where the inner vector is one of your rows, you can use std::insert to perform a concatenation of the rows of your matrices.
vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
You might also find a library such as armadillo useful. I has a function join_rows( A, B ), which is doing, what you ask for. With some chance this will have a better performance, than what you can program yourself.

CUDA - check repeated values and add two values

I have two group of arrays
a1 a2 a3 a4 a5 a6 a7 a8 <= name it as key1
b1 b2 b3 b4 b5 b6 b7 b8 <= val1
c1 c2 c3 c4 c5 c6 c7 c8
and
d1 d2 d3 d4 d5 d6 d7 d8 <= key2
e1 e2 e3 e4 e5 e6 e7 e8 <= val2
f1 f2 f3 f4 f5 f6 f7 f8
The arrays a1,...,an and d1,...,dn are sorted and might be repeated. i.e. their values might be something like 1 1 2 3 4 6 7 7 7 ... I want to check if for each Tuple di,ei check if it is equal to any of ai,bi. If it is (di==ai,bi==ei) then I have to combine fi and ci using some function e.g. add and store in fi.
Firstly, is it possible to do this using zip iterators and transformation in thurst library to solve this efficiently?
Secondly, the simplest method that I can imagine is to count occurance of number of each keys (ai) do prefix sum and use both to get start and end index of each keys and then for each di use above counting to iterate through those indices and check if ei==di. and perform the transformation.
i.e. If I have
1 1 2 3 5 6 7
2 3 4 5 2 4 6
2 4 5 6 7 8 5
as first array, I count the occurance of 1,2,3,4,5,6,7,...:
2 1 1 0 1 1 1 <=name it as count
and then do prefix sum to get:
2 3 4 4 5 6 7 <= name it as cumsum
and use this to do:
for each element di,
for i in (cumsum[di] -count[di]) to cumsum[di]:
if ei==val1[i] then performAddition;
What I fear is that since not all threads are equal, this will lead to warp divergence, and I may not have efficient performance.
You could treat your data as two key-value tables.Table1: (a,b) -> c and Table2: (d,e)->f, where pair (a,b) and (d,e) are keys, and c, f are values.
Then your problem simplifies to
foreach key in Table2
if key in Table1
Table2[key] += Table1[key]
Suppose a and b have limited ranges and are positive, such as unsigned char, a simple way to combine a and b into one key is
unsigned short key = (unsigned short)(a) * 256 + b;
If the range of key is still not too large as in the above example, you could create your Table1 as
int Table1[65536];
Checking if key in Table1 becomes
if (Table1[key] != INVALID_VALUE)
....
With all these restrictions, implementation with thrust should be very simple.
Similar combining method could still be used if a and b have larger range like int.
But if the range of key is too large, you have to go to the method suggested by Robert Crovella.

Regular expression sequence matching

Is it possible to create a regular expression to find an incrementing sequence of hex numbers? I am trying to find number sequences (4 numbers long) inside seemingly random hex number strings.
... 59 fd 25 bf b1 b2 b3 b4 39 ca ...
... 35 c1 55 c4 c5 c6 c7 74 92 e1 ...
I was hoping to find the pattern b1 b2 b3 b4 in line 1 and c4 c5 c6 c7 in line 2.
Group matching will find same number sequences... /(\w\w)\1{3}/ will find c4 c4 c4 c4 but I haven't found a way to match the incrementing sequence.
Any ideas?
Regex is used for matching patterns occurring repeatedly not for matching patterns occurring incrementally
You better parse it with your own parser