How do I find the maximum of each dimension in a cell array of matrices? - arrays

I am given a cell array A which consists of matrices of different sizes. For example, I could have a three element cell array where the dimensions for each element are:
A{1} -> 4 x 3
A{2} -> 16 x 4
A{3} -> 5 x 14
How would I traverse through the cell array and return the maximum for each dimension overall? For example, the expected output of this operation with the example A above should give:
[16 14]
This is because by examining the first dimension, the maximum number of rows over the three matrices is 16. Similarly, the maximum number of columns over the three matrices is 14.

My original answer returned the maximum element of the cell. Now including your comments the right code:
knedlsepp basically got it. Minor improvement in performance:
[a(:,1),a(:,2)]=cellfun(#size,A);
max(a)

I guess you are looking for:
max(cell2mat(cellfun(#size,A(:),'uni',0)),[],1)

Related

Array 2d grid problem related to adjacent cell with C language

Write a code to develop a 2D grid of integers. Each of the boxes in the grid contains
an integer. You have to identify the cell whose sum of adjacent cells is maximum.
also, you have to print another 2D grid that contains the sum of adjacent cells
for each existent cell. You should take input from the user, an integer n. Then construct a nxn 2D
array/matrix. Take input from the user about the different elements of the matrix.
Then print two integers, i and j, which will be the index of the cell whose sum of
adjacent cells is maximum.
I wrote a code, but I could not print the adjacents cells grid correctly.
if n=3 and 2d grid elements are: 1 2 3
4 5 6
7 8 9
Then the output should be for adjacent cell grid: 11 19 13
25 40 27
17 31 19

2D array minimum sum of Y elements and just two rows that we can chose to get minimum

With given 2d array[X][Y], i have to find the smallest possible sum of Y elements but:
the sum must be created by using just 2 rows,
each value must be from different index
Example:
for array
7 3 7 9
2 20 10 6
8 8 8 8
Result should be 18, as we get 3 + 7 from 1st row and 2 + 6 from 2nd.
I've been thinking about few hours but i can't figure out how to deal with it.
Try this one here.
Method 1 (Naive Approach): Check every possible submatrix in given 2D
array. This solution requires 4 nested loops and time complexity of
this solution would be O(n^4).
Method 2 (Efficient Approach): Kadane’s algorithm for 1D array can be
used to reduce the time complexity to O(n^3).

Multiplying matrix columns

I have a matrix with n rows and 3 columns, and I should multiply row n column 2 with row n column 3.
So if I have a matrix that looks like this:
1 2 3
4 5 6
7 8 9
Then I should multiply 2 with 3, 5 with 6 and 8 with 9, and create a matrix or an array that holds results:
6
30
72
How can I do that in C?
Since you are interested in learning C, an outline should do :-) The output is going to be a single column vector. Input to your function is a matrix, of some dimension p x q, and two column numbers c1 and c2. You can not skin it at least two ways.
a function that does exactly what your problem asks, iterating x[1..p][c1] and x[1..p][c2] (so loop variable will be row numbers 1..p, and multiply them, producing result[1..p]
a function that returns a column vector from a given matrix, and then another function that does the element-wise product of two vectors as above. This jimho might be a more interesting option.
HTH

Finding whithout loop the next highest and the next smallest number of a number in a one-dimensional table in Matlab

am asking if there is a function in matlab which finds the first max and first min numbers compered to a number in an array
example :
if we have an array A = [1 2 3 4 5 6 7 8 9];
and the number is x=3.4;
How can we find whith a function (and not with a loop) and store the fist min and firts max of x in variables min_x max_x ?
desirable results:
min_x = 3
max_x = 4
Use logical indexing to select the entries of A smaller (larger) than x, and then use max (min):
max(A(A-x<0))
min(A(A-x>0))
I'm interpreting "next highest/smallest" as the closest numbers to x in A, from above and from below.
First you'll probably want to convert the decimal to an array. Here are some suggested ways that you can do this. After you have an array of your digits, you should be able to juse use MATLAB's built in max() and min() functions.

find unique adjacent indices in 2d array

Assume I have a 2d array of objects, N x N. Assume that a pair can be made of every adjacent pair of objects, horizontally, vertically or diagonally. How can I count how many unique pairs there are for any value of N?
For example for N = 2
0 1
2 3
You can get 01 02 03 21 23 31, note that 03 is the same as 30
Is there a formula to determine how many of these pairs there are for a given N, and even better an algorithm for generating these?
Language is not that important but I will be using c++.
Using the below algorithm and eliminating duplicate indices, I get following counts. Not sure what the formula is yet.
For size N=2
Unique pairs is =6
For size N=3
Unique pairs is =20
For size N=4
Unique pairs is =42
For size N=5
Unique pairs is =72
For size N=6
Unique pairs is =110
For size N=7
Unique pairs is =156
For size N=8
Unique pairs is =210
For size N=9
Unique pairs is =272
Interesting, the formula appears to be 2^2+2, 4^2+4, 6^2+6, 8^2+8 ...
I find it easiest to pick a representative object of each type of pair (in other words, the top object of a vertical pair, the left most of a horizontal pair, and take your pick for diagonal pairs). This gives n(n-1) vertical pairs, n(n-1) horizontal pairs, and 2(n-1)^2 diagonal pairs (equal amounts of each variety). That totals up to 2(n-1)(n+n-1)=2(n-1)(2n-1), in agreement with your guess.
Each row has n-1 intra-row pairs and there are n rows.
Each column has n-1 intra-column pairs and there are n columns.
Each adjacent pair of rows have 2*(n-1) diagonal pairs and there are (n-1) adjacent row pairs.
Multiply and add these numbers and you will get your solution.
Here's the fixed formula for counting unique pairs.
(4 C 2)*(N-1)^2 - 2*(N-2)*(N-1)
Basically you just use the approach in dasblinkenlight's answer and subtract the "duplicate" edges. The duplicate edges will always be the edges between quadrants. I've added an explanation for the counting of duplicates below.
Using the original formula (4 C 2) * (N-1)**2 for N > 2, you will count duplicates. What you want to do is subtract these duplicate edges from the calculation.
Let's examine the simplest cases for N > 2. Suppose N = 3.
0-1-2
|x*x|
3*4*5
|x*x|
6-7-8
See the places where I marked an asterisk instead of an edge? Those edges will be counted twice by the formula. We can calculate them by breaking them up into horizontal and vertical edges. The number of vertical edges that are counted twice will be (N-2)*(N-1).
In the case of N=3, this will be 1 * 2 = 2. The number of horizontal edges that are counted twice will also be (N-2)*(N-1).
So if we simply add up the number of duplicate vertical edges and duplicate horizontal edges, we get
(N-2)*(N-1) + (N-2)*(N-1) = 2*(N-2)*(N-1)
We can simply subtract that number from our total to get the right number of edges.
Testing count in Python:
from math import factorial
def choose(n, k):
return factorial(n)/(factorial(k) * factorial(n-k))
for N in range(2, 10):
print choose(4, 2) * (N-1)**2 - 2 * (N-2) * (N-1)
The program prints:
6
20
42
72
110
156
210
272

Resources