/    Sign up×
Community /Pin to ProfileBookmark

Identity Array Creation problem

OK, I’ll admit it … I’ve come up with a new form of senility.
I am trying to create a 2-dimensional square array and turn it into an ‘identy’ matrix.
The example display in the console.log show the correct formation of a zero-ed array,
but when I try to create the identity version it does not compute correctly.

Could someone advise me as to where I am losing it?
The ternary instruction should put a ‘1’ along the diagonal of the blank array.

Here is the code I’m using:
“<!DOCTYPE html><html lang=”en”><head><title> Test Page </title>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width-device-width,initial-scale=1.0, user-scalable=yes”/>
</head><body>

<script>
console.clear();

function sqArray(r=2) {
var c = Array(r).fill(0);
var arr = []; while(arr.push(c) < r);
return arr;
}

function mI (rc) { // identity matrix
var Ident = sqArray(rc);
var r, c, a;
for (r=0; r<rc; r++) {
for (c=0; c<rc; c++) {
a = (r === c) ? 1 : 0;
Ident[r][c] = a;
}
} return Ident;
}
var tst = sqArray(); console.log(JSON.stringify(tst), ‘tt 2×2 Default’);
var msq = sqArray(3); console.log(JSON.stringify(msq), ‘ 3×3 zero filled’);
console.log();
var Ident = mI(3); console.log(JSON.stringify(Ident), ‘ 3×3 identity ???NFG???’);
console.log(JSON.stringify([ [1,0,0], [0,1,0], [0,0,1] ]),’ Expected identity’);

</script>

</body></html>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinMay 13.2020 — @JMRKER#1618394

function mI (rc) { // identity matrix

var Ident = sqArray(rc);

var r, c, a;

for (r=0; r<rc; r++) {

for (c=0; c<rc; c++) {

//console.log(c,rc,r)

a = (r === c) ? 1 : 0;

Ident[r][c] = a;

alert(Ident) // this should tell you what your nested loop is doing

}

}return Ident;

}
Copy linkTweet thisAlerts:
@JMRKERauthorMay 13.2020 — @DaveyErvin

Thanks for the suggestion.

The output looks like this when I substituted a

console.log('test:',JSON.stringify(Ident));

for your alert suggestion.

Console was cleared.<br/>
[[0,0],[0,0]] 2x2 Default <br/>
[[0,0,0],[0,0,0],[0,0,0]] 3x3 zero filled

test: [[1,0,0],[1,0,0],[1,0,0]]<br/>
test: [[0,0,0],[0,0,0],[0,0,0]]<br/>
test: [[0,1,0],[0,1,0],[0,1,0]]<br/>
test: [[0,0,0],[0,0,0],[0,0,0]]<br/>
test: [[0,0,1],[0,0,1],[0,0,1]]<br/>
[[0,0,1],[0,0,1],[0,0,1]] 3x3 identity ???NFG???<br/>
[[1,0,0],[0,1,0],[0,0,1]] Expected identity


It works correctly the 1st test line but looks like it is reset on the second in the loop.


Why is this? I don't see where the repeated (and reset) loop is ocuring.

When I move the suggestion between the 1st and 2nd loop [after the for (c=0 line]

I get the answer I expect, but it reset back to the problem line when returned.

New output:

Console was cleared.<br/>
HTMLtest.html line 52 &gt; scriptElement:7:9<br/>
[[0,0],[0,0]] 2x2 Default<br/>
[[0,0,0],[0,0,0],[0,0,0]] 3x3 zero filled<br/>
test: [[1,0,0],[1,0,0],[1,0,0]]<br/>
test: [[0,1,0],[0,1,0],[0,1,0]]<br/>
test: [[0,0,1],[0,0,1],[0,0,1]]<br/>
[[0,0,1],[0,0,1],[0,0,1]] 3x3 identity ???NFG???<br/>
[[1,0,0],[0,1,0],[0,0,1]] Expected identity


You got me closer, but I'm still missing something. :(
Copy linkTweet thisAlerts:
@JMRKERauthorMay 13.2020 — Ah ha! I found the problem.

It was in the creation of the 'sqArray()'

&lt;!DOCTYPE html&gt;&lt;html lang="en"&gt;&lt;head&gt;&lt;title&gt; Test Page &lt;/title&gt;<br/>
&lt;meta charset="UTF-8"&gt;<br/>
&lt;meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/&gt;<br/>
&lt;/head&gt;&lt;body&gt;

&lt;script&gt;<br/>
console.clear();

function sqArray(r=2) {<br/>
var arr = []; <br/>
while(arr.push(Array(r).fill(0)) &lt; r);<br/>
return arr;<br/>
}

function mI (rc) { // identity matrix<br/>
var Ident = sqArray(rc);<br/>
var r, c, a;<br/>
for (r=0; r&lt;rc; r++) {<br/>
for (c=0; c&lt;rc; c++) {<br/>
a = (r === c) ? 1 : 0;<br/>
Ident[r][c] = a;<br/>
}<br/>
} return Ident;<br/>
}

// test section<br/>
var tst = sqArray(); console.log(JSON.stringify(tst), 'tt 2x2 Default');<br/>
var msq = sqArray(3); console.log(JSON.stringify(msq), ' 3x3 zero filled');<br/>
console.log();<br/>
var Ident = mI(3); console.log(JSON.stringify(Ident), ' 3x3 identity');<br/>
console.log(JSON.stringify([ [1,0,0], [0,1,0], [0,0,1] ]),' Expected identity');

&lt;/script&gt;

&lt;/body&gt;&lt;/html&gt;


I was looking in the wrong part of the code.
Copy linkTweet thisAlerts:
@JMRKERauthorMay 13.2020 — Playing around some more and I found the last code could also be fixed by adding a .slice() to the original function

function sqArray(r=2) {

var c = Array(r).fill(0);

var arr = []; while(arr.push(c.slice()) < r);

// var arr = []; while(arr.push(Array(r).fill(0)) < r); // alternative

return arr;

}

Either way works. :)
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...