Vectorizing multidimensional cell arrays in matlab with colon - matlab

If I have a cell array that is:
example=cell(dim1,dim2,dim3);
I would like to access values in the shape of:
example1{:}{:}{1} = rand(20,1);
How can I do so? It is important to know that I would like to conserve the shape of such cell array, namely, to modify the values but having the same type of multidimensional cell array.

I think now I understood your question.
mymatrix(:,:,:,1)=num2cell(rand(size(mymatrix(:,:,:,1))));
Easiest way to use () indexing which returns a cell, not a comma separated list of the individual elements. On the right side using rand(size(x)) with the same indexing expressions makes sure you get the right amount of elements generated. The left side is a cell, so you have to convert the right side to a cell as well.

Related

Set the value of a cell indirectly n Numbers app

Can a cell other than the cell containing the formula be the target of the results of a formula? If so, how?
I have a large array of Checkbox cells. I use the state of the Checkboxes as references for calculations. Once I have completed that calculation a large number Checkboxes have been selected. I'm looking for a way to reset the Checkboxes to a default state. Resetting them manually can be tedious.
My current thought is to mirror the array, fill it with the default values, and somehow map those to the array of Checkboxes. Here is the rub.
As far as I know, the results of a formula are always in the cell in which the formula appears.
A cell containing a Checkbox cannot contain a formula.
I can use INDIRECT() to Gather the contents of a cell but I haven't seen anything that permits me to indirectly Place a value in a cell.
Anyone have a clue as to how I can indirectly put a value into a cell?

Why is cell increment not detected correctly in LibreOffice Calc

I have a column which I'd like to fill by selecting the top two cells and then drag down the column.
The cell contents are:
=Sheet1.B11
=Sheet1.B31
So when I drag down I expect to see
=Sheet1.B51
=Sheet1.B71
Instead, I get
=Sheet1.B13
=Sheet1.B33
Why is Calc not detecting the increment correctly? Adding more cells manually does not help.
The numbers in cell references are not single numbers which can be used to create a series in this way. In other words: The cell reference B11 is not "B"&11, but even one single cell reference.
To get references from Sheet1.B11 upwards in steps of 20, you could use INDEX like this:
=INDEX($Sheet1.$B$1:$B$100000,11+(ROW(A1)-1)*20)
Put this formula into a cell and fill it down.

Creating a 10*10 grid cell in matlab

I am trying to get a empty 10*10 grid cell in order to fill it up with some outputs afterwards. The code I wrote is:
patch(1:100)=1:100;
mapmatrix= zeros(100,100);
for patchi=1:100,
for cellxi=1:10,
for cellyi=1:10,
mapmatrix(mod(patchi-1,10)*10+cellxi,ceil(patchi/10)*10+cellyi)=patch(patchi);
,end,end,end;
imagesc(mapmatrix)
Does anyone know why I am getting a 10*11 grid cell instead of 10*10?
THX
Obviously, that is because you use different index computing methods for cow and column indexes
mod(patchi-1,10)*10+cellxi
vs
ceil(patchi/10)*10+cellyi

matlab: variable horizontal alignment of text

Text objects in MATLAB contain a horizontal alignment property, which can be assigned a value of left, center, or right. Attempts to assign this property by a vector of alignments of equal length to the vectors of strings and coordinates fails to give the intended behavior.
For instance, a statement of the form :
text([1,1,1]/4,[1,2,3]/4,{'ABC';'BCD';'CDE'})
displays the contents of a length-3 cell array of char objects at the X- and Y-coordinates specified by length-3 double arrays. However, attempting to introduce a length-3 cell array of char objects for independent specification of the horizontal alignment of each text element is syntactically invalid;
e.g.,
text([1,1,1]/4,[1,2,3]/4,{'ABC';'BCD';'CDE'},'HorizontalAlignment',{'left';'center';'right'})
My question concerns whether it is possible to specify the HorizontalAlignment property of MATLAB text objects in a variable manner without resorting to constructs explicitly involving loops and conditionals.
You can't assign multiple property values upon creation, but once you have a vector of handles, you can use the many-to-many form of set() like so:
h = text([1,1,1]/4, [1,2,3]/4, {'ABC';'BCD';'CDE'});
set(h, {'HorizontalAlignment'}, {'left';'center';'right'});
The value array has one row per object, one column per property.

How do I detect empty cells in a cell array?

How do I detect empty cells in a cell array? I know the command to remove the empty cell is a(1) = [], but I can't seem to get MATLAB to automatically detect which cells are empty.
Background:
I preallocated a cell array using a=cell(1,53).
Then I used if exist(filename(i)) and textscan to check for a file, and read it in. As a result, when the filename(i) does not exist, an empty cell results and we move onto the next file.
When I'm finished reading in all the files, I would like to delete the empty cells of a. I tried if a(i)==[]
Use CELLFUN
%# find empty cells
emptyCells = cellfun(#isempty,a);
%# remove empty cells
a(emptyCells) = [];
Note: a(i)==[] won't work. If you want to know whether the the i-th cell is empty, you have to use curly brackets to access the content of the cell. Also, ==[] evaluates to empty, instead of true/false, so you should use the command isempty instead. In short: a(i)==[] should be rewritten as isempty(a{i}).
All above mentioned answers are incorrect, because in my case when i used them, they removed empty cells and then all elements of my cell array situated in a row manner instead of preserving their actual shape. In fact after using this kind of approach your cell array elements tend to be a row cell vector.
I have found this approach which works correctly in my case.
source : https://groups.google.com/forum/#!topic/comp.softsys.matlab/p3NX0fI6u90
approach:
myCellARRAY(all(cellfun(#isempty,myCellARRAY),2), : ) = [];