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

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

Related

Using one array values to access another array elements Matlab

I am doing some 3D and 4D matrix manipulation in Matlab.
I have created a 2D array which row values contain the index values of interest in a 3D matrix.
Assuming array A of size (Nx2)
A=[2 3 1;5 6 2;7 9 3;3 3 4;1 5 5]
2 3 1
5 6 2
7 9 3
3 3 4
1 5 5
Then, I want to use these elements to manipulate matrix B of size (NxMxL)
B=rand(9,9,5);
So I want to set B(2,3,1)=0 which corresponds to A(1,:).
If I naively go B(A(1,:))=0 this doesn't return me the desired output.
What I understand is that Matlab translate this into B=B(:) which reshape the matrix into a 1xNML
and then returns me the elements 2, 3 and 1 of the reshaped matrix.
How can I avoid this and make it understand my argument B(A(1,:))=B(2,3,1)?
use sub2ind , for example zeroing all the elements in B using of the rows in A as indices:
B(sub2ind(size(B),A(:,1),A(:,2),A(:,3)))=0;

I don't understand the required (matlab)

I'm in the beginning of matlab course and trying to do some home work; for the next problem I don't understand what's required. any help?
Write a function called bottom_left that takes two inputs: a matrix N and a scalar n, in that order, where each dimension of N is greater than or equal to n. The function returns the n-by-n square array at the bottom left corner of N.
This is quite simple even for me.
You have a matrix: [1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]
and you have a scalar: 2
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
"The function returns the n-by-n square array at the bottom left corner of N"
N=2
therefore
the output is the 2 x 2 array in the bottom left corner:
9 10
13 14
thats it. The additional info "where each dimension of N is greater than or equal to n" is just to confuse a bit what to do since the input matrix is given and does not needs to be created. Now, being this a homework you can find out how to get such array for any given matrix.
What you need to do first is to test if one of the dimensions of your Matrix N (I'm going to assume it's not a square Matrix) is lower than the scalar n. If both of the dimensions are higher then n, then we need to simply put the left lower block of the Matrix N into the variable out. The last operation is done by Matrix Indexing, which for more information you can visit this Link
function [ out ] = bottom_left( N, n )
[m,b]=size(N); % m is number of rows, b is number of columns
if (min(m,b)>=n) % to test if one of the dimensions is lower then the scalar n
out=N((m-n+1):m,1:n); % extracting the lower left n-by-n bloc from the Matrix by Indexing
end
end

how to eliminate repeating integers from cell array in matlab

i have [words*sentences]matrix where sentences have integers that represent sentence numbers from a text document from this matrix i have constructed 1D array of [1*N] which shows words and in which sentences they occur number wise.
once above step is done i have taken intersection to check which words occur together in which sentences the code is as follows:
OccursTogether = cell(length(Out1));
for ii=1:length(Out1)
for jj=ii+1:length(Out1)
OccursTogether{ii,jj} = intersect(Out1{ii},Out1{jj});
end
end
celldisp(OccursTogether)
the sample output is as follows which shows 1st word occurs in sentence
{5 10 16} same word occurs with 2nd word in sentence {11 12 13} and word 1 occurs with word 3 in sentence {9 14} and so on till the Nth word:
OccursTogether{1,1} = 5 10 16
OccursTogether{2,1} = 5 12 16
OccursTogether{3,1} = 9 14
now what i want is to show output in one line based upon OccursTogether cell array without repeating sentence numbers as below:
output: {5 9 10 12 14 16}
any help would me appreciated..
If I understand correctly:
result = unique([OccursTogether{:}]);
In your example this gives
result =
5 9 10 12 14 16
Here is a way using cellfun and cell2mat. The idea is to vertically concatenate each cell form the cell array, convert it to a matrix and apply the unique function.
So the first step:
a = cellfun(#(x) x.',OccursTogether,'uni',false)
Takes each cell and transpose it, thus making a n x 1 vector. The result is a cell array containing vertical vectors:
a =
[3x1 double]
[3x1 double]
[2x1 double]
Now we can use cell2mat to convert to a numeric matrix since the dimensions will fit and finally apply unique. Otherwise that would not be possible (eg using 1x3 and 1x2 vectors).
In 1 line that looks like this:
output = unique(cell2mat(cellfun(#(x) x.',OccursTogether,'uni',false)))
output =
5
9
10
12
14
16
Hope this is what you meant! If not please tell me.

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

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)

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

Resources