multidimensional cell array in Matlab - arrays

I'v 3 cell arrays of same number of cells, However I want to reduce in only one cell array having multiple dimensions so that they can be access by column wise like array's column. I'm working in Matlab's environment, However I'v tried to do so, but unfortunately I'v found no cell access as column as in matrices. Any Suggestions for handle such case?
My Code:
P = cell(1,10);
Pd = cell(1,10);
Pdd = cell(1,10);
for ii=1:10
P{ii}= [repmat([0 0 0],2,1)];
Pd{ii} = [repmat([1 1 1],2,1)];
Pdd{ii} = [repmat([2 2 2],2,1)];
end

Concatenate the three cell arrays vertically:
Pall = [P; Pd; Pdd]
This gives a 3x10 cell array Pall, such that Pall(1,:) is P, etc.
If you want to avoid creating multiple cell arrays from the beginning: do something similar to this:
for ii=1:10
Pall{1,ii}= [repmat([0 0 0],2,1)];
Pall{2,ii} = [repmat([1 1 1],2,1)];
Pall{3,ii} = [repmat([2 2 2],2,1)];
end
To avoid loops, with the values in your example:
Pall = mat2cell(repmat([repmat([0 0 0],2,1); repmat([1 1 1],2,1); repmat([2 2 2],2,1)],1,10),[2 2 2],3*ones(1,10))

Related

Nesting arrays in MATLAB

arr=[ [],[],[] ];
for i=1:3
arr(i)=[i,i+1];
end
Expected output: arr=[[1,2],[2,3],[3,4]];
I am trying to put multiple arrays into a single array and I have tried the above code but it does not work
How can I nest arrays?
What you are trying is very Pythonic, nesting lists in lists. This doesn't work in MATLAB. You have basically two easy options: a multi-dimensional array, or a cell array with arrays.
arr = zeros(3,2); % initialise empty array
arr_cell = cell(3,1); % initialise cell
for ii = 1:3
arr(ii,:) = [ii, ii+1]; % store output as columns
arr_cell{ii} = [ii, ii+1]; % store cells
end
arr =
1 2
2 3
3 4
celldisp(arr_cell)
arr_cell{1} =
1 2
arr_cell{2} =
2 3
arr_cell{3} =
3 4
Cells can take various sized (or even types of) arguments, whereas a multi-dimensional matrix has to have the same number of columns on each row. This makes a cell array more flexible, but the numerical array is a lot faster and has more numerical functions available to it.
A small side-note, I suggest to not use i as a variable name in MATLAB, as it's a built-in function.

Multiply elements in second column according to labels in the first

I'm working in Matlab.
I have a two-dimensional matrix with two columns. Lets consider elements in the first column as labels. Labels may be repeated.
How to multiply all elements in the second column for every label?
Example:
matrix = [1,3,3,1,5; 2,3,7,8,3]'
I need to get:
a = [1,3,5; 16,21,3]'
Can you help me with doing it without for-while cycles?
I would use accumarray. The preprocessing with unique assigns integer indices 1:n to the values in the first row, which allow accumarray to work without creating unnecessary bins for 2 and 4. It also enables the support for negative numbers and floats.
[ulable,~,uindex]=unique(matrix(:,1))
r=accumarray(uindex,matrix(:,2),[],#prod)
r=[ulable,r]
/You can also use splitapply:
[ulable,~,uindex]=unique(matrix(:,1))
r=splitapply(#prod,matrix(:,2),uindex)
r=[ulable,r]
You can do it without loops using accumarray and the prod function:
clear
clc
matrix = [1,3,3,1,5; 2,3,7,8,3]';
A = unique(matrix,'rows');
group = A(:,1);
data = A(:,2);
indices = [group ones(size(group))];
prods = accumarray(indices, data,[],#prod); %// As mentionned by #Daniel. My previous answer had a function handle but there is no need for that here since prod is already defined in Matlab.
a = nonzeros(prods)
Out = [unique(group) a]
Out =
1 16
3 21
5 3
Check Lauren blog's post here, accumarray is quite interesting and powerful!
Try something like this, I'm sure it can be improved...
unValues = unique(matrix(:,1));
bb = ones(size(unValues));
for ii = 1:length(unValues)
bb(ii) = bb(ii)*prod(matrix(matrix(:, 1) == unValues(ii), 2));
end
a = [unValues bb];

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 =

Concatenating 1D matrices of different sizes

I perhaps am going about this wrong, but I have data{1}, data{2}...data{i}. Within each, I have .type1, .type2.... .typeN. The arrays are different lengths, so horizontal concatenation does not work.
For simplicity sake
>> data{1}.type1
ans =
1
2
3
>> data{2}.type1
ans =
2
4
5
6
Results should be [1;2;3;2;4;5;6]
I've been trying to loop it but not sure how? I will have a variable number of files (a,b..). How do I go about looping and concatenating? Ultimately I need a 1xN array of all of this..
My working code, thanks..figured it out..
for i = 1:Types
currentType = nTypes{i}
allData.(currentType)=[];
for j = 1:nData
allData.(currentType) = [allData.(currentType); data{j}.(currentType)(:,3)]; %3rd column
end
end
Look at cat, the first argument is the dimension. In your simple example it would be:
result = cat(1,a,b);
Which is equivalent to:
result = [a;b];
Or you can concatenate them as row vectors and transpose back to a column vector:
result = [a',b']';
For the case of a structure inside a cell array I don't think there will be any way around looping. Let's say you have a cell array with M elements and N "types" as the structure fields for each element. You could do:
M=length(data);
newData=struct;
for i=1:M
for j=1:N
field=sprintf('type%d',j); % //field name
if (M==1), newData.(field)=[]; end % //if this is a new field, create it
newData.(field)=[newData.(field);data{i}.(field)];
end
end

MATLAB: removing some of the elements of a matrix

I have a matrix that looks like :
A =[
1 5
2 10
3 12
4 25
5 8]
Let's assume that I want to remove the rows that contain elements that are larger than 10. I have been trying to use a for loop and simply read the matrix and use
for ii = 1:5
for jj = 2
if A(ii,jj) > 10
A(ii,2) = [];
end
end
end
The problem is that, I keep receiving errors regarding the size of a matrix. As one row is deleted, the size of the matrix is reduced and I know that I have to set size(A)=size(A)-1 but it doesn't work. Any help is appreciated!
You can't remove a single element when using subscript notation, so your error is here:
A(i,2) = [];
You have two choices, either remove the entire row:
A(i,:) = [];
or else linearise your matrix and remove single elements (but then you won't be able to recover you original shape:
for ii = numel(A):-1:1
if A(ii) > 10
A(ii) = [];
end
end
but using a loop for this is unnecessary and probably inefficient. You can do it using logical indexing like so:
A(any(A'>10),:) = [];
to remove the whole row or else to just remove single elements try:
A(A>10) = []
but then you'll see that you end up with a row vector (linearized) result
Here is a solution, quite similar to the one of #Dan. Basically this keeps what you want instead of removing what you don't want:
A(all(A<=10,2),:)

Resources