How to copy cell to array in Matlab - arrays

Alright let me explain detailed my question
this below image is displaying my matrix where i want to copy my data
Alright now what i want to do that is as you can see 1x4 cell
i want to copy it as an array to another variable such as
input_values=ones(1,4);%init
input_values=input_matrix_training(1);
So at the above i am trying to copy the elements in that cell array which is row 1 to the input_values array. But if i do as i above i am getting this instead of the values that array contains. ty
instead of above it should be like

The other values are a cell, and are thus best referenced with {} instead of (). Also, sometimes they need to be wrapped into [], depending on the format. Plus the fact that you don't need to initialize input_values, and what you should do becomes this:
input_values=[input_matrix_training{1}];
Or you can just use cell2mat
input_values=cell2mat(input_values(1));

Related

How can i save an element from a cell array into a normal array in matlab

i have the code below :
if encoded(i)==code1(2,j)
that is a simple line and basically i want to save a specific element from a cell array named code1 into another array named encoded. The problem here is that matlab shows the below message:
Undefined operator == for input arguments of type cell.
Is there any solution to this problem? How can i save an element from a cell array into a normal array in MATLAB?
Use the function cellfun. Depending on the type of data in code1{2,j}, the first argument of the cellfun should be an appropriate function.
For example, if code1{2,j} is a string, use
cellfun(#str2double, code1{2,j})
Alternatively, if code1{2,j} is an array, use cell2mat as
cell2mat(code1{2,j})

Google Scripts: Populating Multi-Dimensional Array With Cell Values

I am learning the basics of Google Scripting after using VBA in Excel for many years, so please forgive the basic question.
I want to take a range of cells in my sheet using getValues() and then create a multi-element array.
Here is a simple example from my code:
var gameInfo = [];
gameInfo = gameMasterSheet.getRange(3, 3, 1, 9).getValues();
As you can see, the defined range is 9 cells in a single row.
My goal is to create an array from these 9 cells, and have each cell be accessible via a separate array element. However, it seems all 9 cell values are being inserted into gameInfo[0], and when I reference gameInfo[2] hoping to obtain the value of the third cell in the range, "undefined" is returned.
Is there a way to use getValues() to populate an array with separate elements? If so, how is this done? If not, what is a better alternative?
Thanks for any help you all can provide!
getValues returns a 2d array, even when it's a single row of cells. Think of it like this: gameInfo[row - 1][column - 1], so the top left is gameInfo[0][0].
All of your data is in gameInfo is in one row (gameInfo[0]), so the third element will be accessed as gameInfo[0][2] (row 1, column 3).
gameInfo[2] would be the third row, which is indeed outside of the range and undefined.
Also: to get just the values into an array from a 2d array, you could do this:
const values = [];
gameInfo.forEach(row => {
row.forEach(column => {
values.push(column);
})
})
See the script at the bottom of the Simple Mail Merge Tutorial for some useful code to retrieve data. Copy all the code from this comment down:
//////////////////////////////////////////////////////////////////////////////////////////
//
// The code below is reused from the 'Reading Spreadsheet data using JavaScript Objects'
// tutorial.
//
/////////
/////////////////////////////////////////////////////////////////////////////////
// getRowsData iterates row by row in the input range and returns an array of objects.
Once you have, use getRowsData to retrieve the data as an array of objects. If I understand you correctly, this will get you want you are after.
Karl S

Using jsonencode with length 1 array

When using the MATLAB jsonencode function it seems very difficult to convert size 1 arrays into the correct JSON format i.e. [value]. For example if I do:
jsonencode(struct('words', [string('hello'), string('bye')]))
Then this produces:
{"words":["hello","bye"]}
which is correct. If however I do:
jsonencode(struct('words', [string('hello')]))
Then it produces:
{"words":"hello"}
losing the square brackets, which it needs because it is in general an array. The same happens when using a cell rather than an array, although using a cell does work if it's not inside a struct.
Any idea how I can work around this issue?
It seems this can be solved by using a cell rather than an array and then not creating the struct inline. Like
s.words = {'hello'};
jsonencode(s)
Output:
{"words":["hello"]}
I presume when created inline the cell functionality of matlab is actually trying to make multiple structs rather than multiple strings. Note that this still won't work with arrays as matlab treats a size one array as a scalar.

How to create single image from cell array of matrices in Matlab?

I needed to split a grayscale image in equal parts so I used the function mat2cell. Then I had to equalize each of the parts separatelly, for this purpose I used the function histeq. I reused the same cell array variable for this. Here is the code:
height=round(size(img,1)/number_of_divisions);
length=round(size(img,2)/number_of_divisions);
M=zeros(number_of_divisions,1);
N=zeros(1,number_of_divisions);
M(1:number_of_divisions)=height;
N(1:number_of_divisions)=length;
aux=mat2cell(img,M,N);
for i=1:size(aux,1)
for j=1:size(aux,2)
aux{i,j}=histeq(aux{i,j},256);
end
end
So now how do I merge each cell into one single image?
Use cell2mat
img2=cell2mat(aux);
For a better performance, replace your code with blockproc
blocksize=ceil(size(img)./number_of_divisions);
img2=blockproc(img,blocksize,#(block_struct)histeq(block_struct.data));

Matlab making new cell array from another cell array fileds

I have a cell array thiscells:
thiscells <1*40 cell>
eacharray of thiscells is 280*3
I have a function (TheFunction) that gets input of cell array that each array is X*2
How can I send a cell array to TheFunction that contain only columns1,3 from each array?
I tried
TheFunction(thiscells{:}(:,1:2)) % tring to send only columns 1,2
But it didn't work
Edit:
working with (but still looking for faster way to do so):
What is did so far
TheFunction(makingNewCells(thiscells,2));
when
makingNewCells:
[newCells]=makingNewCells(oldCells)
for ii=1:max(size(oldcells))
newCells=oldCells(:,[1 column]);
end
This question and its answers may be of help - thiscells{:}(:,1:2) is already failing without the call to Thefunction, if I am not mistaken.
For cells, the problem is even worse: add to this impossibility to double-subref that even if it did what you intended it to do, this passes 40 arguments (which are all 280x2 vectors) to Thefunction. I doubt there are (m)any legitimate uses for a function that takes 40 arguments of the same type, instead of having those wrapped in a cell. I would probably try doc cellfun first.
EDIT: just to be clear, I suggest changing Thefunction so that it accepts a cell of 40 matrices of size 280x2), then using
cell_of_twocolumned_arrays = cellfun(#(x)x(:,[1 2]),thiscells)

Resources