which structure I should use for this matlab arrays - arrays

I'm starting to develop a project which uses multi-dimensional arrays very often.
my arrays are mostly 2 , 3 dimensional or so.
As a 2D array sample consider 'A', I may have 2 or more 1D arrays in a cell.
sth like
A=[1, [78,9] [10,65], 9;
2 , 3 , 6;
7 , [9,1] , [91,41,96][10,-1]]
As you saw in 'A(1,2)' there are two 1D arrays.
I don't know which structure I should use to achieve such thing.
moreover I want to be able to have access to all those 1D arrays.
please share your knowledge with me.

try using cell or struct i would recommend cell.
E.g. preinitialize A1:
A1=cell(3,3)
(that would be a 3x3 cell array/matrix). Then you can adress elements with curly brackets ({}). E.g.:
A1{1,1}= 1;
A1(1,1)={1};
both work. You can also define many cells in one line. E.g:
A1(2,:) = {2,3,6};
For the cases with multiarray entries use another cell structure:
B= {[78,9], [10,65]};
A1(1,2) = {B};
and so on. Pay attention to use curly brackets around B (or the coordinate in A1)! Otherwise he would try to merge the cells from B in A1 but that won't do any good because B is a 1x2 cell and you want to give it as argument to one cell in A1.
If you want to return the value inside a cell you have to use curly brackets again:
A1{1,1}
would return 1.

It depends on what you want to do with such a structure. You could use cell arrays for each 1d array entry, and create a matrix of such cell arrays:
a = {1, 2};
b = {-1, 4, 6};
M = [a b];
Alternatively, you can define a sparse 3d array.

Related

How to get the mean of first values in arrays in matrix in Matlab

If I have a square matrix of arrays such as:
[1,2], [2,3]
[5,9], [1,4]
And I want to get the mean of the first values in the arrays of each row such:
1.5
3
Is this possible in Matlab?
I've used the mean(matrix, 2) command to do this with a matrix of single values, but I'm not sure how to extend this to deal with the arrays.
Get the first elements in all arrays of matrix, then call mean function
mean(matrix(:,:,1))
maybe you need to reshape before call mean
a = matrix(:,:,1);
mean(a(:))
You can apply mean function inside mean function to get the total mean value of the 2D array at index 1. You can do similary with array at index 2. Consider the following snapshot.
After staring at your problem for a long time, it looks like your input is a 3D matrix where each row of your formatting corresponds to a 2D matrix slice. Therefore, in proper MATLAB syntax, your matrix is actually:
M = cat(3, [1,2; 2,3], [5,9; 1,4]);
We thus get:
>> M = cat(3, [1,2; 2,3], [5,9; 1,4])
M(:,:,1) =
1 2
2 3
M(:,:,2) =
5 9
1 4
The first slice is the matrix [1,2; 2,3] and the second slice is [5,9; 1,4]. From what it looks like, you would like the mean of only the first column of every slice and return this as a single vector of values. Therefore, use the mean function and index into the first column for all rows and slices. This will unfortunately become a singleton 3D array so you'll need to squeeze out the singleton dimensions.
Without further ado:
O = squeeze(mean(M(:,1,:)))
We thus get:
>> O = squeeze(mean(M(:,1,:)))
O =
1.5000
3.0000

Applying Movmedian Within Cell Array

I have a cell array (2 x 6) called "output", each cell in row #1 {1 -> 6, 2} contains a 1024 x 1024 x 100 matrix. I want to apply movmedian to each cell in row #1. I would like to apply this function in dimension = 3 with window size = 5.
output = cellfun(#movmedian(5,3), output,'uniform', 0);
This is the code that I have come up with so far, however, it produces an "unbalenced or unexpected parenthesis or bracket" error. I am unsure what is causing this error. I am also somewhat unsure how to instruct matlab to perform this operation only on row 1 of the cell array, please help!
Thank you for your time!!
The function handle passed as the first argument to cellfun will be sequentially passed the contents of each cell (i.e. each 3-D matrix). Since you need to also pass the additional parameters needed by movmedian, you should create an anonymous function like so:
#(m) movmedian(m, 5, 3)
Where the input argument m is the 3-D matrix. If you want to apply this to the first row of output, you just have to index the cell array like so:
output(1, :)
This will return a cell array containing the first row of output, with : indicating "all columns". You can use the same index in the assignment if you'd like to store the modified matrices back in the same cells of output.
Putting it all together, here's the solution:
output(1, :) = cellfun(#(m) movmedian(m, 5, 3), output(1, :),...
'UniformOutput', false);
...and a little trick to avoid having to specify 'UniformOutput', false is to encapsulate the results of the anonymous function in a cell array:
output(1, :) = cellfun(#(m) {movmedian(m, 5, 3)}, output(1, :));

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.

How can I check whether an element is in a nested cell array?

How can I check whether an element is in a nested cell array?
ex:
A = {{4 5 6};{6 7 8}};
b = 5;
The function
ismember(b,A{1})
does not work.
Is there any solution better than for-loop?
Because each element is a cell, you don't have a choice but to use cellfun combined with ismember, which is the same as using a loop in any case. Your cells are specifically two-deep (per Andrew Janke). Each cell element in your cell array is another cell array of individual elements, so there is no vectorized solution that can help you out of this.
Assuming that each cell is just a 1-D cell array of individual elements, you would thus do:
A = {{4 5 6};{6 7 8}};
b = 5;
out = cellfun(#(x) ismember(b, cell2mat(x)), A);
Which gives us:
out =
1
0
This checks to see if the value b is in each of the nested cell arrays. If it is your intention to simply check for its existence over the entire nested cell array, use any on the output, and so:
out = any(cellfun(#(x) ismember(b, cell2mat(x)), A));
Because each cell element is a cell array of individual elements, I converted these to a numerical vector by cell2mat before invoking ismember.

How to use cell arrays in Matlab?

I am a beginner at using Matlab and came across cell arrays but I am not sure how to use indexing for it.
I have created a cell array of 5 rows and 3 cols by doing the following:
A = cell(5,3);
Now is it possible to go through the cell array by row first and then col like how a nested for loop for a normal array?
for i=1:5
for j=1:3
A{i,j} = {"random"} //random numbers/ string etc
end
end
With cell arrays you have two methods of indexing namely parenthesis (i.e. (...)) and braces (i.e. {...}).
Lets create a cell array to use for examples:
A = {3, 9, 'a';
'B', [2,4], 0};
Indexing using paranthesis returns a portion of the cell array AS A CELL ARRAY. For example
A(:,3)
returns a 2-by-1 cell array
ans =
'a'
0
Indexing using braces return the CONTENTS of that cell, for example
A{1,3}
returns a single character
ans =
a
You can use parenthesis to return a single cell as well but it will still be a cell. You can also use braces to return multiple cells but these return as comma separated lists, which is a bit more advanced.
When assigning to a cell, very similar concepts apply. If you're assigning using parenthesis, then you must assign a cell matrix of the appropriate size:
A(:,1) = {1,1}
if you assign a single value using parenthesis, then you must put it in a cell (i.e. A(1) = 2 will give you an error, so you must do A(1) = {2}). So it's better to use braces as then you are directly affecting the contents of the cell. So it is correct to go
A{1} = 2
this is equivalent to A(1) = {2}. Note that A{1} = {2}, which is what you've done, will not give a error but what is does is nests a cell within your cell which is unlikely what you were after.
Lastly, if you have an matrix inside one of your cells, then Matlab allows you to index directly into that matrix like so:
A{2,2}(1)
ans =
3
for example:
for i=1:5
for j=1:3
A{i,j} = rand(3)
end
end
should work perfectly fine
just skip the { } on the right side of the =

Resources