How to get number of column in an Array array? - arrays

If I have a two-dimensional array, how can I get the number of columns ?
I can get number of lines with Array.length(o) where o is an 5*10 matrix, which returns 5.
But how to do with columns?

Assuming there is at least one "line", and that the column arrays are of equal length, just take the length of the first "column" array:
Array.length m.(0)
If there are no lines however, this will raise an exception.

Related

Get specific cells from a cell array

I have a numeric array sized 1000x1 which have values 0 and 1, called conditionArray.
I have a cell array called netNames with the same size (1000x1) and its cells contain string values (which are name of some circuit nets).
I want to extract net names which from netNames which their pairwise condition bit is 1 in conditionArray.
E.g. if conditionArray(100) is equal to extract its net name from netNames{100}.
Output of this process can be stored in an string array or cell array.
Are there any ways to do this operation with pairwise operations or I should use a for statement for this?
You shall check out cellfun in Matlab anytime you want to manipulate each element inside a cellarray without using a for loop.
As I understand, you have:
N = 1000;
% an array with 0s and 1s (this generates random 0s and 1s):
conditionArray = randi([0,1],N);
% a cell array with strings (this generates random 5-character strings):
netNames = cell(N);
netNames = cellfun(#(c)char(randi([double('a'),double('z')],1,5)), netNames, 'UniformOutput',false);
To extract the elements from netNames where conditionArray is 1, you can do:
netNames(conditionArray==1)
This uses logical indexing into the cell array.

How to divide one array with unknown number of values into sub-arrays in matlab

I have an array in MATLAB which the total number of elements is unknown, but it is always multiple of 32. How can I transform this array into a cell array in which each of its positions will contain 32 values?
You can use num2cell:
a = rand(1,32*5);
result = num2cell(reshape(a,[],32),2);

Mapping a 2D array into 1D array with variable column width

I know mapping 2D array into 1D array has been asked many times, but I did not find a solution that would fit a where the column count varies.
So I want get a 1-dimensional index from this 2-dimensional array
Col> _0____1____2__
Row 0 |_0__|_1__|_2__|
V 1 |_3__|_4__|
2 |_5__|_6__|_7__|
3 |_8__|_9__|
4 |_10_|_11_|_12_|
5 |_13_|_14_|
The normal formula index = row * columns + column does not work, since after the 2nd row the index is out of place.
What is the correct formula here?
EDIT:
The specific issue is that I have a list of items in with the layout like in the grid, but a one dimensional array for the data. So while looping through the elements in the UI, I need to get the correct data, but can only get the row and column for that element. I need to find a way to turn a row/column value into an index for the data-array
Bad picture trying to explain it
A truly optimal answer (or even a provably correct one) will depend on the language you are using and how it lays out memory for such arrays.
However, taking your question simply at face value, you have to know what the actual length of each row is in order to calculate a 1D index.
So either the row length follows some pattern that can be inferred from the data, or you have (or can write) a rlen = rowLength( 2dTable, RowNumber) function.
Then, depending on how big the tables are and how fast you need to run, you can calculate a 1D index from the 2d table by adding all the previous row lengths until the current row length is less than the 2d column index.
or build a 1d table of the row lengths (or commulative rowlengths) so you can scan it and so only call your rowlength function for each row only once.
With a better description of your problem, you might get a better answer...
For your example which alternates between 3 and 2 columns you can construct a formula:
index = (row / 2) * (3 + 2) + (row % 2 ? 3 : 0) + column
(C-like syntax, assuming integer division)
In general though, the one and only way to implement what you're doing here, jagged arrays, is to make an array of arrays, a.k.a. an Iliffe vector. That means, use the row number as index into an array of pointers which point to the individual row arrays containing the actual data.
You can have an additional 1D array having the length of the columns say "length". Then your formula is index=sum {length(i)}+column. i runs from 0 to row.

length in 2 dimension array

var example is a 2-dimension array. example.length will give values like 14.3
But how can I get an integer for the length of example in second dimension, like 3 in this case?
Thank you!
If the array is homogeneous (which is always the case when such an array is the result of a getValues() call in a spreadsheet range for example) you can simply write :
example[0].length
EDIT : a few comments to be more clear ...
The 2D array you get from example = range.getValues() is always an array of rows data.
The number of rows is represented by example.length and the inner array length (representing rows content) is always example[0].length, which is actually the number of columns

Finding maximum index of a row of array in MATLAB

I want to get the index of a specific row in a 2-D array. I have a 15*15 array (Q), and I want to get index of the maximum number of 2nd row.
I wrote this code, but an error occurred:
y=Q(2,:);
x=max(y)
??? Subscript indices must either be real positive integers or logicals.
You are getting the maximum value of the second row, but you want the index of the maximum value. Here's how to get the index:
[~, index] = max(y)

Resources