Array size in NxN matrix - Swift [duplicate] - arrays

This question already has answers here:
Two-dimensional array in Swift
(11 answers)
Closed 3 years ago.
May I know how I can get the array size of NxN matrix
like number of rows and no: of columns where my input is 4x4
arr[4][4]
Is it something like arr[N].count for rows and arr[][N].count for columns?
Please advice

Using the built-in Array type, you cannot create a two-dimensional Array (which you refer to as matrix) that's guaranteed to be "NxN", because you cannot guarantee that all inner arrays will have the same number of elements and you also cannot guarantee that the number of elements in the inner arrays will be the same as the number of arrays in the two-dimensional array.
However, you can access the number of inner arrays (or "rows") using arr.count and you can access the number of elements in the inner arrays using arr[i].count (which will be the number of "columns" assuming that all inner arrays have the same number of elements).
If you'll be using two-dimensional Arrays a lot and you want to guarantee that all of the inner arrays have the same number of elements, I'd suggest creating a custom Collection.

You can use count or endIndex to get the dimensions but you need to check each row separately.
var matrix: [[Int]] = [[1,2,3], [4,5,6,7], [8,9]]
print(matrix.count)
for i in 0..<matrix.endIndex {
print(matrix[i].endIndex)
}

Related

Python numpy: Selecting array entries based on input array [duplicate]

This question already has answers here:
Getting the indices of several elements in a NumPy array at once
(5 answers)
Closed 2 years ago.
Assume I have an array:
a = np.array([1,2,3,4,5])
Now I want to find the indices of elements in this array corresponding to the values given by another array input:
input = np.array([2,4,5])
The expected result should be:
result = [1,3,4]
A boolean mask, which is true for element indices 1,3,4 would also be fine.
I do not want to use looping to solve this. I assume that a possible solution has to do with the numpy where() function, but using this one, I am only able to compare the entries of array a with one element of array input at a time. Because the length of input might differ, I cannot really use this approach. Do you have any other ideas?
Thanks in advance.
np.where(np.in1d(a, inp))[0]
or:
np.isin(a, inp).nonzero()[0]
or as suggested here:
sorter = np.argsort(a)
sorter[np.searchsorted(a, inp, sorter=sorter)]
output:
[1 3 4]
np.where(np.in1d(a, inp))[0] np.where(np.in1d(a, inp))[0]

how to access an element of an n-D matrix where index comes from a mathematical operation [duplicate]

This question already has answers here:
MATLAB: Accessing an element of a multidimensional array with a list
(2 answers)
Use a vector as an index to a matrix
(3 answers)
Closed 5 years ago.
How can I access an element of an n-D matrix where index comes from a mathematical operation in Matlab?
For example I have a 4D Matrix called A.
I want to access element 1,1,1,1 which results from (3,4,5,6) - (2,3,4,5)
Is there any way I can do this assuming that the array can be any dimension d and that the array from subtraction will always be d elements long?
One possible way would be to utilise the fact that MATLAB can use linear indexing for any n-dimensional array as well as row-column type indexing. Then you just have to calculate the linear index of your operation result.
There may be a more elegant way to do this but if x is the array holding the result of your operation, then the following works
element = A(sum((x-1).*(size(A).^[0:length(size(A))-1]))+1);
The sub2ind function feels like it should help here, but doesn't seem to.
Another approach is to converting to a cell array, then to a comma-separated list:
A = rand(3,4,5,6); % example A
t = [2 1 3 4]; % example index
u = num2cell(t);
result = A(u{:});

Extract one dimension from a multidimensional array [duplicate]

This question already has answers here:
On shape-agnostic slicing of ndarrays
(2 answers)
Closed 6 years ago.
Suppose A is multi-dimensional array (MDA) of size 3,4,5 and B is another MDA of size 3,4,5,6.
I know A(1,:,:) or B(1,:,:,:) can both extract their elements along the first dimension.
I now need to write a general program to extract the k-th dimension from a MDA without knowing its size.
For example, the MDA C has 6 dimension: 4,5,6,7,8,9 and I want an extraction C(:,:,k,:,:,:).
Sometimes, the MDA 'D' has 4 dimension: 3,4,5,6 and I want another extraction D(k,:,:,:).
That is, my problem is the numbers of colon is varying because of the dimension.
Thanks in advance
You can use string arrays to index the array dynamically:
function out = extract(arr,dim,k)
subses = repmat({':'}, [1 ndims(arr)]);
subses(dim) = num2cell(k);
out = arr(subses{:});
where dim is the dimension in which you want to select and k is an index within that dimension.
I have used a code from this answer:
https://stackoverflow.com/a/27975910/3399825

Correlation of the positions of a multidimensional array to what they represent during initialization

I first want to clarify if my logic in the following is correct:
In a 2 dimensional array, say A[x][y], x represents the number of braces and y the number of elements in each brace. So int A[2][3] is initialized as:
{
{1,2,3}, //from A[0][0] to A[0][2];
{4,5,6} //from A[1]{0] to A[1][2];
};
Second, I want to know what the similar correlations are in a 3 dimensional array, four dimensional, and so on, and how to know which dimension number correlates to what level for any multi-dimensional array.
For example, in A[3][4][2], does the 3 denote the number of 2d tables, or rows/columns in each table? And in A[2][3][4][5], does 2 represent the number of 2d tables, no. of 3d tables, no. of 1d tables, or no. of rows/columns per 1d table? Note:I'm getting my head around multi dimensional arrays for the first time, please explain as simplistically as possible.
Yes what you say it's correct. You can think that recursively.
Start from a 1D array (let's assume that it has 3 elements):
int 1darray[] = {0, 1, 2};
Now producing a 2D array simply says go inside every element of 1darray and put another 1D array, to produce a 2D one, like this:
int 2darray[] = {1darray_0, 1darray_1, 1darray_2};
where the 1darray_0, 1darray_1, 1darray_2 are 1D arrays, just like the 1darray we created in the start. So now this will form a 3x3 2D array.
Now the 3D array can be formed like this:
int 3darray[] = {2darray_0, 2darray_1, 2darray_2};
where the 2darray_0, 2darray_1, 2darray_2 are 2D arrays, just like the 2darray we created above. So now this will form a 3x3x3 3D array.
Your example:
A[3][4][2]
says that A has:
3 rows
4 columns
2 z-columns
In general however, I would advice you to have in mind the picture I have in my 2D dynamic array (C):
which describes in a nutshell what I tried to explain in the start.
As you increase your dimensions, you replace every element of the previous array with an array of the next dimension, while you reach the end.

F# - assigning element in (jagged) 2d-array sets several elements [duplicate]

This question already has an answer here:
Array.create and jagged array
(1 answer)
Closed 6 years ago.
I'm experiencing some behavior in f# that i do not understand. I was trying to create a 2d table using nested arrays. The sub-arrays will have the same length, so I could have used Array2D. However later on I will need the table rows as normal arrays, so in order to avoid conversion from a multidimensional to a regular array I want to represent the table as a jagged array.
The following code is an example of how I initialize and assign elements in the table.
let table = Array.create 3 (Array.zeroCreate<int> 2);;
table.[0].[0] <- 1;;
I would expect this piece of code to set the first element in the first row. However, what it actually does is setting the first element in all three rows.
table;;
val it : int [] [] = [|[|1; 0|]; [|1; 0|]; [|1; 0|]|]
Why does table.[0].[0] set the first element in all three sub-arrays? I tried finding the memory addresses of the sub-arrays using System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement, and it seems to me that they are different, so they three rows are not the same array. What is going on here?
When you use array.create, it takes as an argument an object, not a function.
As a result, each element of the jagged array is a reference to the same array, so you get the observed behaviour.
Just use a function other than array.create to make the array

Resources