Arduino UNO loops Serial unexpectedly, when doing more complex programs - c++

I have a piece of code like this:
#include<LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void leftShift(bool toShift[28], int noOfShifts, bool destination[28]) {
for (int i = 0; i < 28; i++) {
destination[(i - noOfShifts + 28) % 28] = toShift[i];
}
}
void divideBinary(bool binary[], size_t sizeOfBinary, bool LB[], bool RB[]) {
size_t half = sizeOfBinary / 2;
// LB - first half
size_t i = 0;
for (; i < half; i++) {
LB[i] = binary[i];
}
// RB - second half
for (; i < half * 2; i++) {
RB[i - half] = binary[i];
}
}
void createSubkeys(bool binaryKey[8 * 8], bool subkeys[16][48]) {
Serial.println("just entered subkeys"); Serial.flush();
int pc_1[56] = {
57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4
};
bool keyPermutation[56];
// according to pc_1 create from 64-bit key 56-bit keyPermutation
for (int i = 0; i < 56; i++) {
keyPermutation[i] = binaryKey[pc_1[i] - 1];
}
// C and D will be saved here: [C/D] [index] [28 bools]
bool CD[2][16 + 1][56 / 2];
Serial.println("CD ready"); Serial.flush();
// divide keyPermutation into halves to C0 a D0 - each consists of 28 bits
divideBinary(keyPermutation, 56, CD[0][0], CD[1][0]);
// from C0, D0 and shifts make C1, D1 -> C16, D16
int shifts[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
for (int i = 1; i < 17; i++) {
leftShift(CD[0][i - 1], shifts[i - 1], CD[0][i]);
leftShift(CD[1][i - 1], shifts[i - 1], CD[1][i]);
}
// each subKey out of 16 is made from one out of 16 CD with the use of pc_2
int pc_2[48] = {
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
};
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 48; j++) {
// find out which part of CD we should look at - that means C, or D? for C CorD is 0, for D 1
int where = pc_2[j] - 1;
bool CorD = 0;
if (where >= 56 / 2) {
CorD = 1;
where -= 56 / 2; // subtract 28, to start indexing from 0 again in case of D
}
subkeys[i][j] = CD[CorD][i + 1][where];
}
}
// Serial.println("subkeys ready");
}
void setup() {
// put your setup code here, to run once:
Serial.begin( 9600 );
lcd.begin(16, 2);
Serial.println("ready"); Serial.flush();
bool binaryKey[8 * 8];
bool subkeys[16][48];
createSubkeys(binaryKey, subkeys);
}
void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0,0);
lcd.print("haf");
}
It is not really important what it does, it is just so you can roughly see its complexity.
Why won't this work on Arduino, even if it were to be much slower? Instead, when I run it, my Arduino UNO really behaves weirdly. In Serial it just repeats a sequence of characters "jready" in a loop. Forever. It never prints anything else and it never reaches the loop() function.
My Arduino and its Serial both work perfectly fine for smaller programs.
EDIT: If I attempt to locate the problem by commenting out blocks of code, it seems to occur here:
// from C0, D0 and shifts make C1, D1 -> C16, D16
int shifts[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
for (int i = 1; i < 17; i++) {
leftShift(CD[0][i - 1], shifts[i - 1], CD[0][i]);
leftShift(CD[1][i - 1], shifts[i - 1], CD[1][i]);
}
But if I make my setup() function more complex, it happens earlier, on this line:
divideBinary(keyPermutation, 56, CD[0][0], CD[1][0]);

You are passing variables to methods without correctly specifying whether they should return that value which you modify inside that method. Look at:
void leftShift(bool toShift[28], int noOfShifts, bool destination[28])
You modify destination in there but destination is boolean array copied to the method but when the method finishes that modified value is not returned to the caller. Change your declarations.
Here:
bool CD[2][16 + 1][56 / 2];
You have a third dimension to the array but never use it. The third dimension has 28 elements but you only ever use the second dimension. If you are trying to do pointer operations then you will have to change leftShift
Also consider the above points I made with divideBinary.

Related

Recursive solution of ordered Coin Combinations II (CSES)

Question Link
Consider a money system consisting of n coins. Each coin has a positive integer value. Your task is to calculate the number of distinct ordered ways you can produce a money sum x using the available coins.
For example, if the coins are {2,3,5} and the desired sum is 9, there are 3 ways:
2+2+5
3+3+3
2+2+2+3
I am trying to write recursive solution for this.
I came up with this code.
#include<iostream>
#include<vector>
using namespace std;
#define M 1000000007
vector<int>arr;
vector<vector<int>>dp;
int solve(int i,int target){
if(i>=(int)arr.size()) return 0;
if(target<0) return 0;
if(target==0) return 1;
if(dp[i][target]!=-1) return dp[i][target];
return dp[i][target]=(solve(i,target-arr[i])%M+solve(i+1,target)%M)%M;
}
int main(){
int n,target;
cin>>n>>target;
arr.resize(n);
dp.resize(n+1,vector<int>(target+1,-1));
for(int i=0;i<n;++i){
cin>>arr[i];
}
cout<<solve(0,target);
}
but this code is giving time-limit-exceeded. How can this code be written in a recursive manner so that it gets accepted? Here is non-recursive approach but I want to know how to write a recursive version of this.
an example of recursive sum which select results would be:
void recursive_sum( IN const std::vector<int>& coins,
IN int result,
OUT std::vector<std::string>& res,
IN std::deque<int> numbers = {},
IN int actual_sum = 0)
{
for(auto& n: coins)
{
int nsum = actual_sum + n;
if(nsum == result)
insert_result(res, copy_insert(numbers, n));
else if(nsum < result)
recursive_sum(coins, result, res, copy_insert(numbers, n), nsum);
}
}
a main similar to this would do the job:
int main()
{
std::vector<std::string> res{};
std::vector<int> coins{};
int sum_result = 0;
input_coins(coins);
input_sum_result(sum_result);
recursive_sum(coins, sum_result, res);
out_result(res);
return 0;
}
if you want to take a look to a full version of this example click here.
output example:
"
Insert a number or 'next' to continue
10
Actually coins are: { 10, }
Insert a number or 'next' to continue
15
Actually coins are: { 10, 15, }
Insert a number or 'next' to continue
25
Actually coins are: { 10, 15, 25, }
Insert a number or 'next' to continue
next
Insert the sum result:
100
Possible sums are:
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, }
{10, 10, 10, 10, 10, 10, 10, 15, 15, }
{10, 10, 10, 10, 10, 10, 15, 15, 10, }
{10, 10, 10, 10, 10, 10, 15, 25, }
{10, 10, 10, 10, 10, 10, 25, 15, }
{10, 10, 10, 10, 10, 25, 10, 15, }
{10, 10, 10, 10, 10, 25, 25, }
{10, 10, 10, 10, 15, 15, 15, 15, }
{10, 10, 10, 15, 15, 10, 15, 15, }
{10, 10, 10, 15, 15, 15, 25, }
{10, 10, 10, 15, 25, 15, 15, }
{10, 10, 15, 15, 10, 25, 15, }
{10, 10, 15, 15, 15, 10, 10, 15, }
{10, 10, 15, 15, 15, 10, 25, }
{10, 10, 15, 15, 25, 25, }
{10, 15, 15, 15, 15, 15, 15, }
{10, 15, 15, 25, 10, 25, }
{10, 15, 25, 25, 10, 15, }
{10, 15, 25, 25, 25, }
{10, 25, 10, 10, 10, 10, 25, }
{10, 25, 10, 10, 15, 10, 10, 10, }
{15, 15, 15, 15, 15, 25, }
{15, 25, 15, 15, 15, 15, }
{25, 25, 25, 10, 15, }
{25, 25, 25, 15, 10, }
{25, 25, 25, 25, }
"

AVX2 integer multiply of signed 8-bit elements, producing signed 16-bit results?

I have two __m256i vectors, filled with 32 8-bit integers. Something like this:
__int8 *a0 = new __int8[32] {2};
__int8 *a1 = new __int8[32] {3};
__m256i v0 = _mm256_loadu_si256((__m256i*)a0);
__m256i v1 = _mm256_loadu_si256((__m256i*)a1);
How can i multiply these vectors, using something like _mm256_mul_epi8(v0, v1) (which does not exist) or any another way?
I want 2 vectors of results, because the output element width is twice the input element width. Or something that works similarly to _mm_mul_epu32 would be ok, using only the even input elements (0, 2, 4, etc.)
You want the result separated in two vectors so this is my suggestion for your question. I've tried to be clear, simple and realizable:
#include <stdio.h>
#include <x86intrin.h>
void _mm256_print_epi8(__m256i );
void _mm256_print_epi16(__m256i );
void _mm256_mul_epi8(__m256i , __m256i , __m256i* , __m256i* );
int main()
{
char a0[32] = {1, 2, 3, -4, 5, 6, 7, 8, 9, -10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, -24, 25, 26, 27, 28, 29, 30, 31, 32};
char a1[32] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, -13, 14, 15, 16, 17, 18, 19, -20, 21, 22, 23, 24, -25, 26, 27, 28, 29, 30, 31, 32, 33};
__m256i v0 = _mm256_loadu_si256((__m256i*) &a0[0]);
__m256i v1 = _mm256_loadu_si256((__m256i*) &a1[0]);
__m256i r0, r1;//for 16 bit results
_mm256_mul_epi8(v0, v1, &r0, &r1);
printf("\nv0 = ");_mm256_print_epi8(v0);
printf("\nv1 = ");_mm256_print_epi8(v1);
printf("\nr0 = ");_mm256_print_epi16(r0);
printf("\nr1 = ");_mm256_print_epi16(r1);
printf("\nfinished\n");
return 0;
}
//v0 and v1 are 8 bit input vectors. r0 and r1 are 18 bit results of multiplications
void _mm256_mul_epi8(__m256i v0, __m256i v1, __m256i* r0, __m256i* r1)
{
__m256i tmp0, tmp1;
__m128i m128_v0, m128_v1;
m128_v0 = _mm256_extractf128_si256 (v0, 0);
m128_v1 = _mm256_extractf128_si256 (v1, 0);
tmp0= _mm256_cvtepi8_epi16 (m128_v0); //printf("\ntmp0 = ");_mm256_print_epi16(tmp0);
tmp1= _mm256_cvtepi8_epi16 (m128_v1); //printf("\ntmp1 = ");_mm256_print_epi16(tmp1);
*r0 =_mm256_mullo_epi16(tmp0, tmp1);
m128_v0 = _mm256_extractf128_si256 (v0, 1);
m128_v1 = _mm256_extractf128_si256 (v1, 1);
tmp0= _mm256_cvtepi8_epi16 (m128_v0); //printf("\ntmp0 = ");_mm256_print_epi16(tmp0);
tmp1= _mm256_cvtepi8_epi16 (m128_v1); //printf("\ntmp1 = ");_mm256_print_epi16(tmp1);
*r1 =_mm256_mullo_epi16(tmp0, tmp1);
}
void _mm256_print_epi8(__m256i vec)
{
char temp[32];
_mm256_storeu_si256((__m256i*)&temp[0], vec);
int i;
for(i=0; i<32; i++)
printf(" %3i,", temp[i]);
}
void _mm256_print_epi16(__m256i vec)
{
short temp[16];
_mm256_storeu_si256((__m256i*)&temp[0], vec);
int i;
for(i=0; i<16; i++)
printf(" %3i,", temp[i]);
}
The output is:
[martin#mrt Stack over flow]$ gcc -O2 -march=native mul_epi8.c -o out
[martin#mrt Stack over flow]$ ./out
v0 = 1, 2, 3, -4, 5, 6, 7, 8, 9, -10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, -24, 25, 26, 27, 28, 29, 30, 31, 32,
v1 = 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, -13, 14, 15, 16, 17, 18, 19, -20, 21, 22, 23, 24, -25, 26, 27, 28, 29, 30, 31, 32, 33,
r0 = 2, 6, 12, -20, 30, 42, 56, 72, 90, -110, 132, -156, 182, 210, 240, 272,
r1 = 306, 342, -380, 420, 462, 506, 552, 600, 650, 702, 756, 812, 870, 930, 992, 1056,
finished
[martin#mrt Stack over flow]$
NOTE: I've commented the intermediate results tmp0 and tmp1 in the recommended code.
In addition, as peter suggested in comments and provided a godbolt link, if your program loads from memory and you don't need to multiply elements in vectors you can use this code:
#include <immintrin.h>
//v0 and v1 are 8 bit input vectors. r0 and r1 are 18 bit results of multiplications
__m256i mul_epi8_to_16(__m128i v0, __m128i v1)
{
__m256i tmp0 = _mm256_cvtepi8_epi16 (v0); //printf("\ntmp0 = ");_mm256_print_epi16(tmp0);
__m256i tmp1 = _mm256_cvtepi8_epi16 (v1); //printf("\ntmp1 = ");_mm256_print_epi16(tmp1);
return _mm256_mullo_epi16(tmp0, tmp1);
}
__m256i mul_epi8_to_16_memsrc(char *__restrict a, char *__restrict b){
__m128i v0 = _mm_loadu_si128((__m128i*) a);
__m128i v1 = _mm_loadu_si128((__m128i*) b);
return mul_epi8_to_16(v0, v1);
}
int main()
{
char a0[32] = {1, 2, 3, -4, 5, 6, 7, 8, 9, -10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, -24, 25, 26, 27, 28, 29, 30, 31, 32};
char a1[32] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, -13, 14, 15, 16, 17, 18, 19, -20, 21, 22, 23, 24, -25, 26, 27, 28, 29, 30, 31, 32, 33};
__m256i r0 = mul_epi8_to_16_memsrc(a0, a1);
}

Iterate through arraylist calculating the difference of consecutive values in python

i'm trying to iterate over an arraylist saving in every loop the highest/lowest difference of the consecutive values.
e1=([ 0 , 0, 0, 0, 15, 28, 28, 28, 27, 27, 35, 44, 43, 43, 42, 39])
Hodiffmax = 0
Hodiffmin = 0
for k in e1:
diff1= e1[k+1] - e1[k]
if diff1 > Hodiffmax:
Hodiffmax=diff1
if diff1 < Hodiffmin:
Hodiffmin=diff1
The problem is i get an "index out of bound" error. How can i iterate through an arraylist with [k+1]? I tried a bunch of things now but i dont get smarter. I appreciate any help!
EDIT (that works neither):
for k in e1:
for w in k:
diff1= e1[w+1] - e1[w]
if diff1 > Hodiffmax:
Hodiffmax=diff1
if diff1 < Hodiffmin:
Hodiffmin=diff1
Error: for w in k - TypeError: 'numpy.int32' object is not iterable
With [y - x for x, y in zip(e1, e1[1:])] you can get consecutive differences without worrying for the indexes:
>>> e1 = [ 0 , 0, 0, 0, 15, 28, 28, 28, 27, 27, 35, 44, 43, 43, 42, 39]
>>> l = [y - x for x, y in zip(e1, e1[1:])]
>>> Hodiffmax, Hodiffmin = max(l), min(l)
>>> Hodiffmax, Hodiffmin
15, -3
Use the grouper recipe:
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
from itertools import izip_longest # required by grouper
i = [0, 0, 0, 0, 15, 28, 28, 28, 27, 27, 35, 44, 43, 43, 42, 39]
lowest = None
highest = None
for z,q in grouper(i, 2):
v = z-q
if v < lowest:
lowest = v
if v > highest:
highest = v
print(lowest)
print(highest)
The problem here is that you are iterating over the elements of the list. By doing
for k in e1:
k will get the values of the elements on e1. k=0, k=0, k=0, k=0, k=15, k=28 and so on. What you want instead is to iterate over the range of the list.
for k in range(len(e1)):
k will get the values of indexes on e1. k=0, k=1, k=2, k=3, k=4, k=5 and so on. I think you were looking for something like this:
e1 = [0, 0, 0, 0, 15, 28, 28, 28, 27, 27, 35, 44, 43, 43, 42, 39]
for k in range(len(e1)):
print k
if k > 0:
diff1 = e1[k] - e1[k-1]
if diff1 > Hodiffmax:
Hodiffmax=diff1
if diff1 < Hodiffmin:
Hodiffmin=diff1
print 'Hodiffmax ' + str(Hodiffmax)
print 'Hodiffmin ' + str(Hodiffmin)

Storing values from STL vector to another vector

I stuck at one point and need some help.
I have a STL vector with the following values:
[1, 17, 2, 18, 3, 19, 1, 17, 2, 18, 3, 19, 1, 17, 2, 18, 3, 19].
note that first six values in a vector (i.e. 1, 17, 2, 18, 3, 19 ) can be considered as one block. So this vector has 3 blocks each with the values as described above.
Now, I want to organize this vector in a following way:
[1, 17, 1, 17, 1, 17, 2, 18, 2, 18, 2, 18, 3, 19, 3, 19, 3, 19]
.
So essentially I am picking first two values (i.e. 1, 17) from each block first and store them sequentially 3 times (basically # of blocks which in this case is 3). I then go on to pick next two values (i.e. 2, 18) and continue.
How do I achieve this..?
Any help will be greatly appreciated.
Thanks
Sound quite easy once you figure out the exact mapping. So external loop is the number of chunks in every block since that's the number of final groups, middle loop goes over each original block while inner loop just goes through every element of a chunk. Final result should be something like (untested):
std::vector organized;
organized.reserve(data.size());
const int blockSize = 6;
const int subBlockSize = 2;
assert(data.size()%blockCount == 0 && blockSize%subBlockSize == 0);
const int blockCount = data.size()/blockSize;
const int subBlockCount = blockSize/subBlockSize;
for (int i = 0; i < subBlockCount; ++i)
for (int j = 0; j < blockCount; ++j)
for (int k = 0; k < subBlockSize; ++k)
organized.push_back(subBlockSize*i + blockSize*j + k);
Just create a function shuffle(i) that takes an index into the new array, and returns an index from the original array:
#include <iostream>
#include <cstdlib>
using namespace std;
int list[] = { 1, 17, 2, 18, 3, 19, 1, 17, 2, 18, 3, 19, 1, 17, 2, 18, 3, 19 };
int shuffle( int i )
{
div_t a = div( i, 6 );
div_t b = div( a.rem, 2 );
return 2*a.quot + 6*b.quot + b.rem;
}
int main()
{
for( int i=0 ; i<18 ; ++i ) cout << list[shuffle(i)] << ' ';
cout << endl;
return 0;
}
This outputs:
1 17 1 17 1 17 2 18 2 18 2 18 3 19 3 19 3 19
Just allocate the new vector, and fill it from the old one:
new_vector[i] = list[shuffle(i)];

Setting up a Counter in a Table

I have two questions,
Q1.
The code is below:
orgtable = Table[{i, node2 = i + 1, node3 = node2 + 6, node4 = node3 - 1,
node5 = i + 18, node6 = node5 + 1, node7 = node6 + 6,
node8 = node7 - 1}, {i, 1, 36}
];
modtable = Drop[orgtable, {6, 36, 6}];
finaltable = With[{n = 5, m = 10},Flatten[Partition[modtable, n, n + m, 1, {}], 1]]
The first piece of code gives me an original table, the second one gives me a modified table, and the third yields the final table.
The output of the final table looks like this:
{{1, 2, 8, 7, 19, 20, 26, 25}, {2, 3, 9, 8, 20, 21, 27, 26},
{3, 4, 10, 9, 21, 22, 28, 27}, {4, 5, 11, 10, 22, 23, 29, 28},
{5, 6, 12,11, 23, 24, 30, 29}, {19, 20, 26, 25, 37, 38, 44,43},
{20, 21, 27,26, 38, 39, 45, 44}, {21, 22, 28, 27, 39, 40, 46, 45},
{22, 23, 29,28, 40,41, 47, 46}, {23, 24, 30, 29, 41, 42, 48, 47}}
But I want it to set up a counter to the final table so that my output should look like this(below):The counter will increase by 1 and in the below example it will start with 200;
{{200,1, 2, 8, 7, 19, 20, 26, 25}, {201,2, 3, 9, 8, 20, 21, 27, 26},
{202,3, 4,10, 9, 21,22, 28, 27}, {203,4, 5, 11, 10, 22, 23, 29, 28},
{204,5, 6, 12,11, 23, 24, 30, 29} and so on
As you can see from the desired output the count is present for each element and increases by one
Now question number two:
mycounter = 100;
tryone =
TableForm[
Flatten[
Table[{++mycounter, xcord, ycord,
(150*(Sin[((xcord - 90*2*3.14)/180]^2)*
(Sin[((ycord - 45)*2*3.14)/180]^2)
) + 20
}, {xcord, 0, 200, 5}, {ycord, 0, 200, 5}
], 1
]
]
In the above example, I have successfully implemented a counter which is starting from 100 and incrementing by 1 and it gives me an output
100 0 0 20.03
101 0 5 20.04 and so on..
But now I want to use the Transpose function on this, since I want to transpose the value presented but at the same time I don't want to transpose the "my counter".
mycounter = 100;
secondtry=
TableForm[
Flatten[
Transpose[
Table[{++mycounter, xcord, ycord,
(150*(Sin[((xcord - 90)*2*3.14)/180]^2)*
(Sin[((ycord - 45)*2*3.14)/180]^2)
) +20}, {xcord, 0, 200, 5}, {ycord, 0, 200, 5}
]
], 1
]
]
But as you can see the Transpose function transposes also the "mycounter" which I do not want. How do you prevent the transpose function from working on "mycounter" but work on the rest of it?
Any other idea of implementing a counter in the above code is also welcome.
Removed answer to first question as I probably didn't understand what you wanted.
As to the second question: I'm not sure whether I fully understand you here. If the counter belongs to the coordinate set the output should be left as it is, how awkward it may look. If the counter column is simply a line counter of the final output you could put in after you have done your flattening just like before.
But in this case, it seems the Transpose is fully superfluous. It suffices to switch the order of the indices of your table. If you do that you can leave your counter as it is:
mycounter = 100;
secondtry =
Flatten[
Table[{mycounter++, xcord,ycord,
(150*(Sin[((xcord - 90)*2*3.14)/180]^2)*
(Sin[((ycord - 45)*2*3.14)/180]^2)
) + 20},
{ycord,0, 200, 5}, {xcord, 0, 200, 5} (* order switched here *)
], 1
]
A few notes: I removed the TableForm from your assignment. This is generally only used for printing and not for data that gets assigned to a variable. If you want to do an assignment and want to see the result at the same time you could try something like
(myVar = Table[...{...},{...}] ) //TableForm
Also note that you don't have to multiply by 3.14/180 to convert degrees to radians. Mathematica has a built-in quantity named Degree for that (if you use the shortcut esc deg esc you will have a nice degree symbol instead). It looks like you are multiplying with 2 pi/180 for this conversion. If that was your intention, it was incorrect. The conversion is either 2 pi/360 or pi/180. ((xcord - 90)*2*3.14)/180 should then be written as (xcord - 90)Degree.
Question 1 :
Transpose[Prepend[Transpose[#], Range[Length[#]] + 200]] &#
{{1, 2, 8, 7, 19, 20, 26, 25}, {2, 3, 9, 8, 20, 21, 27, 26}, {3, 4,
10, 9, 21, 22, 28, 27}, {4, 5, 11, 10, 22, 23, 29, 28}, {5, 6, 12,
11, 23, 24, 30, 29}, {19, 20, 26, 25, 37, 38, 44, 43}, {20, 21, 27,
26, 38, 39, 45, 44}, {21, 22, 28, 27, 39, 40, 46, 45}, {22, 23,
29, 28, 40, 41, 47, 46}, {23, 24, 30, 29, 41, 42, 48, 47}}
Question2:
Function[mat,
Partition[
Transpose[Prepend[Transpose[#], Range[Length[#]] + 99]] &#
Flatten[mat, 1], Length[mat]]]#
Table[{xcord,
ycord, (150*(Sin[((xcord - 90)*2*3.14)/
180]^2)*(Sin[((ycord - 45)*2*3.14)/180]^2)
) + 20
}, {xcord, 0, 200, 50}, {ycord, 0, 200, 50}
]
Create the rest of the table without the counter, create a suitable n*1 matrix of the index using Range, and then use MapThread with the inner function Join to put the two together.
Your finaltable could also be produced from modtable using Table as follows:
finaltableAlt = Delete[#, Transpose#{Flatten#Table[i + j, {i, 5, (
Length[#] - 10), 15}, {j, 10}]}] & # modtable
Another possibility for numbering:
MapIndexed[Flatten#{#2[[1]] + 199, #1} &, finaltableAlt]