Rauf
May 15, 2020, 8:54pm
1
Hi I want to create a matrix ‘B’ like given below while I only have an array ‘A’
A=[0 1 2 3 4]
B= [0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8]
For some reason, these few lines are not working
for (var i=0;i<5;i=i+1){
for (var j=0;j<5;j=j+1){
B[i][j] = A[j]+i;
}
}
Any help or suggestion will be appreciated.
Thanks
how are you defining A
and B
?
Rauf:
B[i][j]
also if B[i]
is undefined, you need to define it somewhere before doing the above or it will not work
Rauf
May 15, 2020, 9:00pm
3
I have only defined it as
var B = ;
If its not enough, please tell me what else should i do ?
ilenia
May 16, 2020, 11:19am
4
you can’t add an element to something that is undefined.
B[i]
is undefined so you can’t do B[i][j] = A[j]+i
, so you need to define B[i]
before entering the loop