How to resolve the algorithm Spiral matrix step by step in the R programming language

Published on 12 May 2024 09:40 PM
#R

How to resolve the algorithm Spiral matrix step by step in the R programming language

Table of Contents

Problem Statement

Produce a spiral array.

A   spiral array   is a square arrangement of the first   N2   natural numbers,   where the numbers increase sequentially as you go around the edges of the array spiraling inwards.

For example, given   5,   produce this array:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Spiral matrix step by step in the R programming language

Source code in the r programming language

spiral <- function(n) matrix(order(cumsum(rep(rep_len(c(1, n, -1, -n), 2 * n - 1), n - seq(2 * n - 1) %/% 2))), n, byrow = T) - 1

spiral(5)

spiral_matrix <- function(n) {
    spiralv <- function(v) {
        n <- sqrt(length(v))
        if (n != floor(n))
            stop("length of v should be a square of an integer")
        if (n == 0)
            stop("v should be of positive length")
        if (n == 1)
            m <- matrix(v, 1, 1)
        else
            m <- rbind(v[1:n], cbind(spiralv(v[(2 * n):(n^2)])[(n - 1):1, (n - 1):1], v[(n + 1):(2 * n - 1)]))
        m
    }
    spiralv(1:(n^2))
}

spiralMatrix <- function(n)
{
  spiral <- matrix(0, nrow = n, ncol = n)
  firstNumToWrite <- 0
  neededLength <- n
  startPt <- cbind(1, 0)#(1, 0) is needed for the first call to writeRight to work. We need to start in row 1.
  writingDirectionIndex <- 0
  #These two functions select a collection of adjacent elements and replaces them with the needed sequence.
  #This heavily uses R's vector recycling rules.
  writeDown <- function(seq) spiral[startPt[1] + seq, startPt[2]] <<- seq_len(neededLength) - 1 + firstNumToWrite
  writeRight <- function(seq) spiral[startPt[1], startPt[2] + seq] <<- seq_len(neededLength) - 1 + firstNumToWrite
  while(firstNumToWrite != n^2)
  {
    writingDirectionIndex <- writingDirectionIndex %% 4 + 1
    seq <- seq_len(neededLength)
    switch(writingDirectionIndex,
           writeRight(seq),
           writeDown(seq),
           writeRight(-seq),
           writeDown(-seq))
    if(writingDirectionIndex %% 2) neededLength <- neededLength - 1
    max <- max(spiral)
    firstNumToWrite <- max + 1
    startPt <- which(max == spiral, arr.ind = TRUE)
  }
  spiral
}

  

You may also check:How to resolve the algorithm Assertions step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Random number generator (included) step by step in the Racket programming language
You may also check:How to resolve the algorithm Determine if a string is squeezable step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Arrays step by step in the OxygenBasic programming language
You may also check:How to resolve the algorithm Order disjoint list items step by step in the Java programming language