I have an cell including array as below format
a{x,y,z}(i,j)
a is 3 dimensional cell
and
each cell have i*j array
a <79x95x68 cell>
val(:,:,1) =
Columns 1 through 2
[6x6 double] [6x6 double]
[6x6 double] [6x6 double]
[6x6 double] [6x6 double]
i want to rearrange that as below format
a{i,j}(x,y,z)
how to? any good idea? i have to do iteration?
matlab say, a{:,:}(x,y,z) is bad cell referencing.........
here's a suboptimal way to go, it isn't memory efficient, but it is pretty straightforward:
say that x=79,y=95,z=68 are the dimensions of your original cell array, and that each of them gives the same dimensionality ixj matrix (6 by 6). So first you can make a matrix out of the cell array:
b=horzcat(a{:});
then we can reshape it to a 5-d array (this is just for pedagogical purposes) so you already have it ordered as (i,j,x,y,z)...
c=reshape(b,6,6,x,y,z);
then you can either work with c(i,j,x,y,z), or assign c to a cellarray:
d=cell(6,6);
for i=1:6
for j=1:6
d{i,j}=squeeze(c(i,j,:,:,:));
end
end
Related
[EDIT]
Say, I have a cell array A consisting of n cells and each of them is a zero vector of size (m,1).
Example: A is a 1x2 cell array A = [100x1 double] [100x1
double]
How can I insert new values to each cells, on the same rows and at the same time without having to write separate lines of code? Can I use cellfun to do so?
For instance, how can I replace the ithrow of the first cell with the new value m and the ithrow of the second cell with the new value n, using a single line of code?
m and n may or may not be the same values.
as in comments noted you may try other options such as array for its efficiency, however as an answer to the question you can use evalin function and using change_cell you can for example change contents of second row fo each cell to the vals just in one line of code!:
A = {[1 2 3],[4 5 6]};
change_cell = #(rownum , vals) evalin(...
'base', ...
[...
'rownum =', int2str(rownum),';',...
'vals=[', num2str(vals),'];',...
'for i = 1:length(A);',...
'A{i}(rownum)=vals(i);',...
'end;'...
]);
change_cell(2, [6 8])
I am working on a Semi Markov model. In it my task is to find the interval transition probability which is a recursive procedure.
The code that I used is below. For this to run the initial condition is F(0)=eye(3,3) matrix.
I am unable to call this initial value in a loop. Is the code that I have written is right? I need a proper suggestion.
Other data used is
C =
Columns 1 through 4
[3x3 double] [3x3 double] [3x3 double] [3x3 double]
Column 5
[3x3 double]
Y =
Columns 1 through 4
[3x3 double] [3x3 double] [3x3 double] [3x3 double]
Column 5
[3x3 double]
The code:
F=mat2cell(zeros(3,15),[3],[3,3,3,3,3])
for j=1:5
for m=1:j
if (j-m)==0
F{:,j}=eye(3,3)
end
F{:,j}=Y{:,j}+sum(C{:,m}*F{:,(j-m)})
end
end
While you overwrite F{:,j} when j-m==0, you still try to access F{:,(j-m)} later on. Doesn't it say "Cell contents indices must be greater than 0" or "Subscript indices must either be real positive integers or logicals."? Matlab arrays and cells are indexed from 1 upwards.
You might need something like
if (j-m)==0
F{:,j}=eye(3,3);
else
F{:,j}=Y{:,j}+sum(C{:,m}*F{:,(j-m)});
end
Don't forget your semicolons or you'll get loads of unnecessary output.
And do you really need cells? What you wrote could easily be implemented using multidimensional arrays, in which case your colons are actually needed. In your specific example, I think they are redundant, it would suffice to write, for instance, F{j-m}.
I want to change a matrix N*123456 to a cells of cells, each sub-cell contains a N*L matrix
Eg:
matrixSize= 50*123456
N=50
L=100
Output will be 1*1235 cell and each cell has a 50*L matrix (last cell has only 50*56)
I know there is a function mat2cell in matlab:
Output = mat2cell(x, [50], [100,100,100,......56])
But it doesn't sound an intuitive solution.
So is there a good solution?
If I understand you correctly, assuming your matrix is denoted m, this is what you wanted:
a=num2cell(reshape(m(:,1:size(m,2)-mod(size(m,2),L)),N*L,[]),1);
a=cellfun(#(n) reshape(n,N,L), a,'UniformOutput',false);
a{end+1}=m(:,end-mod(size(m,2),L)+1:end);
(this can be shortened to a single line if you wish)...
Lets test with some minimal numbers:
m=rand(50,334);
N=50;
L=100;
yields:
a =
[50x100 double] [50x100 double] [50x100 double] [50x34 double]
note that I didn't check for the exact dimension in the reshape, so you may need to reshape to ...,[],N*L) etc.
Just use elementary maths.
q = floor(123456/100);
r = rem(123456,100);
Output = mat2cell(x, 50, [repmat(100,1,q),r])
I have a double value, A, which is
[1,4,7,6]
I also have B, which is an array that contains many more values. I have a new variable, C, which is essentially a double value of all these numbers (all of them in one cell, vs. five).
[1,4,7,6]
[2,6,9,12]
[3,1,17,13]
[5,7,13,19]
[1,5,9,15]
How do I remove the elements (not the actual values) from C? I want to end up with this.
[2,6,9,12,3,1,17,13,5,7,13,19,1,5,9,15]
How do I get this? I've used these commands:
C(A) = [];
and
C = C(setdiff(1:length(C),A));
The problem is that when I run that command, I get this instead of what I want.
[4,7,2,12,3,1,17,13,5,7,13,19,1,5,9,15]
Clearly that isn't the same as what I have. It's throwing off the rest of my results and I need to fix this specific issue.
Thanks in advance :)
EDIT:
So I figured out that it's spewing the CORRECT numbers out, just in the wrong order. I have to sort it in order for it to work correctly. This is a problem because it causes the next command to be non-functional because the ismember command has issues with the removal (I don't know why, I'm still working on it).
Double array case
If B is a double array, you can use setdiff with 'rows' and 'stable' options, like so -
C = reshape(setdiff(B,A,'rows','stable').',1,[])
With ismember, you can perform the same operation, like so -
C = reshape(B(~ismember(B,A,'rows'),:).',1,[])
You can also use a bsxfun approach as suggested by #Amro -
C = reshape(B(~all(bsxfun(#eq, B, A),2),:).',1,[])
Cell array case
If B is a cell array with number of elements in each cell equal to the number of elements in A, then you can firstly convert it to a double array - B = vertcat(B{:}) and then use either of the above mentioned tools.
Or you can use a cellfun based approach that avoids conversion to a double array, like so -
excl_rows = B(~cellfun(#(x1,x2) isequal(x1,x2), B, repmat({A},size(B,1),1)),:)
C = horzcat(excl_rows{:})
Or another cellfun based approach that avoids repmat -
exclB = B(~cellfun(#(x1) isequal(x1,A), B),:)
C = horzcat(exclB{:})
Example with explanation -
%// Inputs
A = [1,4,7,6]
B = {[1,4,7,6]
[2,6,9,12]
[3,1,17,13]
[5,7,13,19]
[1,5,9,15]}
%// Compare each cell of B with A for equality.
%// The output must be a binary array where one would be for cells that have
%// elements same as A and zero otherwise.
ind = cellfun(#(x1) isequal(x1,A), B)
%// Thus, ~ind would be a binary array where one would reperesent unequal
%// cells that are to be selected in B for the final output.
exclB = B(~ind)
%// exclB is still a cell array with the cells that are different from A.
%// So, concatenate the elements from exclB into a vector as requested.
C = horzcat(exclB{:})
Output -
A =
1 4 7 6
B =
[1x4 double]
[1x4 double]
[1x4 double]
[1x4 double]
[1x4 double]
ind =
1
0
0
0
0
exclB =
[1x4 double]
[1x4 double]
[1x4 double]
[1x4 double]
C =
2 6 9 12 3 1 17 13 5 7 13 19 1 5 9 15
Thanks in advance for the help.
I have an array of values that looks like this
[[1x5 double]; [1x2 double]; ....]
I would like to convert this to this
['12345'; '12'; ....]
cell2mat sort of does what I want, but I end up getting this
[['1' '2' '3' '4' '5']; ['1' '2'];...]
I have been all over the matlab documentation and haven't found a way to do this. Really all I want is to convert [1x5 double] to a string (I can't convert to a num because I don't want to drop insignificant zeros). Is there an simple easy way to do this besides doing this by hand with for loops?
If input_array is your input array, try this cellfun + num2str based approach -
cellfun(#(x) num2str(x,'%1d'), input_array,'uni',0)
Example -
%// Input array
input_array = {randi(9,1,5)-1;randi(9,1,2)-1}
%// Display the cell array values for verification of results later on
celldisp(input_array)
%// Output
out = cellfun(#(x) num2str(x,'%1d'), input_array,'uni',0)
Output (on run) -
input_array =
[1x5 double]
[1x2 double]
input_array{1} =
3 6 0 5 3
input_array{2} =
6 2
out =
'36053'
'62'