logo
down
shadow

Accessing multi-dimensional array with one-dimensional index


Accessing multi-dimensional array with one-dimensional index

By : aspra
Date : November 20 2020, 09:01 AM
it fixes the issue If you want not just to read the values but also set them in the same order as foreach traverses the array, you can use the following general indexer class:
code :
public class ArrayIndexer
{
    readonly int totalLength;
    readonly int lastIndexLength;
    readonly int[] lengths;
    readonly int[] lowerBounds;
    int current;
    readonly int[] currentZeroBased;

    public ArrayIndexer(int[] lengths, int[] lowerBounds)
    {
        lastIndexLength = lengths[lengths.Length - 1];
        totalLength = lengths[0];
        for (int i = 1; i < lengths.Length; i++)
        {
            totalLength *= lengths[i];
        }
        this.lengths = lengths;
        this.lowerBounds = lowerBounds;
        currentZeroBased = new int[lengths.Length];
        current = -1;
    }

    public bool MoveNext()
    {
        current++;
        if (current != 0)
        {
            int currLastIndex = current % lastIndexLength;
            currentZeroBased[currentZeroBased.Length - 1] = currLastIndex;
            if (currLastIndex == 0)
            {
                for (int i = currentZeroBased.Length - 2; i >= 0; i--)
                {
                    currentZeroBased[i]++;
                    if (currentZeroBased[i] != lengths[i])
                        break;
                    currentZeroBased[i] = 0;
                }
            }
        }
        return current < totalLength;
    }

    public int[] Current
    {
        get
        {
            int[] result = new int[currentZeroBased.Length];
            for (int i = 0; i < result.Length; i++)
            {
                result[i] = currentZeroBased[i] + lowerBounds[i];
            }
            return result;
        }
    }
}
int[,] myArray = new int[2, 2];
ArrayIndexer arrayIndexer = new ArrayIndexer(new[] {2, 2}, new[] {0, 0});
int i = 0;
while (arrayIndexer.MoveNext())
{
    myArray.SetValue(++i, arrayIndexer.Current);
}


Share : facebook icon twitter icon
Finding dimensional index in a multi-dimensional array in R

Finding dimensional index in a multi-dimensional array in R


By : Omar Akash
Date : March 29 2020, 07:55 AM
wish of those help @James's answer is better, but I think the narrow answer to your question (multidimensional equivalent of row()/col()) is slice.index ...
code :
M<- array(dim=c(3,3,3))
slice.index(M,1)/10+slice.index(M,2)^2+sqrt(slice.index(M,3))
d <- do.call(expand.grid,lapply(dim(M),seq)) ## create data.frame of indices
v <- with(d,Var1/10+Var2^2+sqrt(Var3))       ## default names Var1, ... Varn 
dim(v) <- dim(M)                             ## reshape into array
Undefined index when accessing multi-dimensional array

Undefined index when accessing multi-dimensional array


By : Tom
Date : March 29 2020, 07:55 AM
wish of those help $item = $company['product']; // throws undefined index product
In here you are passing the wrong key use $item = $company['products'];
Accessing 3-dimensional matrix by a 2-dimensional index in matlab/octave

Accessing 3-dimensional matrix by a 2-dimensional index in matlab/octave


By : Peter Fisera
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I have a 3 dimensional matrix (3rd dimension represents multiple copies of an M x N grayscale image). , One approach -
code :
%// Get size of 3D input array
[m,n,~] = size(test);     

%// Calculate 2D starting, offset & finally actual indices array
start_idx = bsxfun(@plus,[1:m]',[0:n-1]*m);  %//'
offset_idx = m*n*(max_ix-1);
actual_idx = start_idx + offset_idx;

%// Index into 3D input array to extract specific elements, for desired output
max_val = test(actual_idx)
[m,n,~] = size(test);     
max_val = test( bsxfun(@plus,[1:m]',[0:n-1]*m) + m*n*(max_ix-1) )
test(:,:,1) =
    12    66    75    98
    65    75    24    87
    33    59    74     9
test(:,:,2) =
    37    60    21    21
    37    79     9    39
    69    37    78    56
test(:,:,3) =
    23    16    30    10
    65    79    24    41
    49    11    54    11
test(:,:,4) =
    12    61    70    66
    79    97    76    11
    30    44    44    94
max_ix =
     1     2     2     2
     2     4     1     2
     4     2     3     3
max_val =
    12    60    21    21
    37    97    24    39
    30    37    54    11
How to create single dimensional, two dimensional,three dimensional as well as multi dimensional array using C#?

How to create single dimensional, two dimensional,three dimensional as well as multi dimensional array using C#?


By : Bilal Bhatti
Date : March 29 2020, 07:55 AM
this one helps. If you're used to something like Java then multidimensional arrays are a little different in syntax in C#.
Here's a page describing how to do them in C#. Here's a snippet from said page:
Accessing multi-dimensional array using a one-dimensional index in R

Accessing multi-dimensional array using a one-dimensional index in R


By : MK15
Date : March 29 2020, 07:55 AM
Any of those help With R, you have a choice of indexing with arrays and matrices. You can use the dimensional indexing or you can use vector indexng. Just use:
code :
myArray[123:(123+11) ]
Related Posts Related Posts :
  • Why do I get a COMException when I try to initialize an Interop.Word.Application?
  • Finding subtotal for column in repeater control
  • dropdownlist item selected throw exception (stack empty)
  • How can I list IIS working processes programmatically?
  • How to access nested object from JSON with Json.NET in C#
  • OpenXml DataValidation set predefined List for columns
  • How to find recurring word groups in text with C#?
  • layout conrols to developp universal apps in windows 8.1 pro
  • Adding strings to a list via for loop xna
  • Using Vertex Arrays with OpenTK
  • Optimizing List<string>
  • C# Local variables
  • How can I merge the results of a group by Linq-to-XML query?
  • How to add Report in ASP.NET Project
  • C# How to display all instances of a loop in a label?
  • C# subscribe a function to System.Action that is subscribed to another System.Action: why does order matter?
  • Is there any negative effect to setting SqlCommand's CommandTimeout to a high value?
  • C# - TreeView Context Menu Out of Place when using keyboard
  • How to read stored procedure output and return it as list
  • How do you maintain changes made by VisualState triggers after setting another VisualState?
  • Should I be unit testing my bootstrapper and if so how?
  • using .SingleOrDefault() inside .Where() will raise the following exception:- System.NotSupportedException was unhandled
  • C# How would I fill a Form1 Parameter in the same seperate class
  • Access gridview row's data from LinkButton inside that row
  • Translating excel function to c# Math.pow not resulting in same values
  • Arranging Ten Inputted Numbers into Ascending and Descending order
  • AsEnumerable and Query Syntax
  • How can I defensively code against randomly referencing "Table 0" and null values?
  • Changing xaml code position based on screen size
  • Castle Windsor resolve ienumerable and name
  • Drawing multiple sprites with a for loop xna 4.0
  • For loop to Populate Textblocks
  • Visual Studio 2015: Create an Empty Project
  • What are .NET classes to replace the old MSXML2.ServerXMLHTTP?
  • Handle leak in .Net threads
  • Excel add-in ribbon click events bubbling
  • validation rule needed for only numbers to be entered
  • Parsing complex XML, no descendent elements exists when selecting a named element
  • Web Api - IEnumerable with complex type as param is null
  • Email address input validation
  • .Net AND operator Regular Expression using strings in a document
  • Windows phone 8.1 POST x-www-form-urlencoded not working
  • Is there any way to make my C# XMLManager work?
  • ASP.NET 5 Console Application (package) - How to create DBContext from connection string?
  • How to use form's function in other class C#
  • WPF Zebra EPL2 Printing issue
  • C# use reflection to capture exception throw
  • Convert strings in DataRow to double
  • Xml-Serialization is not working
  • HTML Agility Pack can't find classes with trailing spaces
  • ComputeHash Calls Inexplicably Differ
  • Creating an array that holds both integers and datetime C#
  • GroupBy multiple columns in Linq with Take()
  • Rename multiple symbols at once? (using Roslyn)
  • Cast lambda in delegate
  • DataContractSerializer using default properties
  • LINQ Query Sum in recordset
  • change left-hand boolean value to be opposite of the right hand value
  • Get node from XmlDocument using xpath
  • Why does a Control's BeginInvoke() target delegate never occur after Dispose() is called on the Control?
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co