LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Create a matrix

How can I create matrix like this:
1       2      3 . . . .n-2      n-1        n
2       1      2  . . ..n-3      n-2        n-1
3       2      1 . . . .n-4      n-3        n-2
.........................................................
n-1   n-2   n-3 .....2          1           2
n      n-1   n-2      3          2           1 

0 Kudos
Message 1 of 14
(4,202 Views)

I hate to do homework, if that's what this is.  What have you tried?  My solution uses two For Loops inside one For Loop, with one of the inner ones making the countdown from the left and the other making the count-up part.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 2 of 14
(4,154 Views)

I would​ probably first create a V shaped 1D array going [N, N-1, ... 2, 1, 2, ..., N--1, N], then use a single FOR loop to subset into it with an index based on [I] and fixed length, then autoindex at the output tunnel.

0 Kudos
Message 3 of 14
(4,150 Views)

Ramp 1..N. Loop and replace index 0 on each iteration, rotate array in feedback, and index output.

0 Kudos
Message 4 of 14
(4,136 Views)

Here's probably the most compact solution. 😄

 

Try to figure out why it works. 😉

 

 

Message 5 of 14
(4,122 Views)

@altenbach That's pretty cool. Super fast, too.  I've noticed that rotating arrays is not particularly fast, so this is much better.  I'm not sure there is much time difference between the absolute and increment operations inside vs outside the loop.

0 Kudos
Message 6 of 14
(4,115 Views)

@Mancho00 wrote:

I'm not sure there is much time difference between the absolute and increment operations inside vs outside the loop.


I initially had it inside the inner loop, but identical operations on arrays can potentially utilize SSE instructions for an overall gain by at least a factor of 2, but I am not sure if the compiler would utilize it here. In any case these array operations operate in place, so there should be very little difference. It is even possible that the compiler would generate identical code for the two versions (inside vs. outside) once all internal optimizations are applied. 😄

 

(make sure to disable debugging when benchmarking).

Message 7 of 14
(4,106 Views)

These might scale even better, no inner loop! 😄

 

 

 

Download All
Message 8 of 14
(4,100 Views)

@altenbach wrote:

These might scale even better, no inner loop! 😄

 


Now you're just showing off.Smiley Wink

0 Kudos
Message 9 of 14
(4,093 Views)

Thanks/ I did it with formula node. 

0 Kudos
Message 10 of 14
(4,063 Views)