how to find a differentiation or derivative of each array(>1000*2) in cell consists 7000*1?

Hi, let say, there are 700*1 cells. each cell consists of arrays(3000*2). i would like find the derivative of each cell and find its max and min values. combine derivative values min and max separately. my data(cell) looks like,
this 3000 rows*1 column cell data
{
[190*2] %1st row
[180*2] %2nd row
[186*2]
[195*2]
[182*2]
[183*2]
[122*2]
[183*2]
[100*2]
.......... %3000 row}
my code:-
%here cellvalues{i} are in 3000*1
first_derivative = diff(cellvalues{i}) provides 32*2 double values.
but each array in cell has 190*2/180*2/600*2....? when i run above why i am getting only 32*2 values but the entire cell data has 2000*1?
%try to do derivatives of each cell %other way
du = diff(cellvalues{:}) or
first_derivative = diff(cellvalues{i:end}) gives error or many arguments.

5 Comments

Since each cell is >1000*2, you will have two columns after taking the derivative? How do you define the minimum and maximum value? is it separate or combined minimum and maximum for both columns.
The question is not clear yet. Please post running code.
If you get a [32 x 2] matrix as output of diff(cellvalues{i}), this means that cellvalues{i} is a [33 x 2] matrix. The data you have shown are not "(3000*2)", but e.g. [190 x 2].
It is not clear what your inputs are, what you want to calculate exactly and why you assume, that the cuirrent code is wrong. Please explain more details.
@Ameer, thanks for your reply. before derivative only i have 3000 rows and each row has a arrays and its consists of,example, [190*2]....[450*2]till 3000 rows. let say, i want to find out derivative to the first row of cell{[190*2]]} similarly, 2nd row to till end of cells. then max find, every derivative of each row has some max. value and min value. All 3000 rows has max and min, ie., 3000 max values and 3000 min values(its just integer number). i want to keep all max values separately as well as min.
@jan, thanks for your reply. i updated as question. after derivative the 1st row value should be same dimension as before. for ex:-190*2 after derivative also should be 190*2(values change). so i want to do derivative of each row in the 3000*1cell
@Ram, refer to my answer below. It will help you to separate and organize you required variables.

Sign in to comment.

Answers (2)

Should work just fine:
u = cell(5, 1);
u(:) = {rand(20, 2)};
du1 = diff(u{1})
gives me a 19x2 double.

2 Comments

in my case, i know the arrays(shown in above---long data size). i know if it is single array as you written. but, the cellvalues{:} represents all arrays. when i write cellvalues{:} returns 32*2. combination of all arrays in the cell output(i think).
if i write cellvalues{1} gives 190*2 same as size of array in the cell. and i have to write each cellvalues{2,3,4......20000} in each line, here, i want to get each row array derivative and then find maximum.
Please attach the relevant code so we can help you more easily.

Sign in to comment.

You can process, and organize the data with the following code. It will create a struct array which you can use to easily access all the data.
y = cellfun(@processData, x);
here processData is the following function
function out = processData(x)
out.derivative = diff(x);
out.min = min(out.derivative);
out.max = max(out.derivative);
end
create a .m file with this function and save it in Matlab path and then run the above line.

7 Comments

here x means my cellvalues{:} or cellvalues{1} getting output has
//////////
Error using cellfun
Input #2 expected to be a cell array, was double instead.
and also i have written y2-y1/(x2-x1 formula to find the derivative of each point to point. if i write like that, then my code will very long for only finiding derivative!!
1) You need to use cellvalues, the complete cell array. Not some other like cellvalues{:} or cellvalues{1}.
Also, it seems that you want to calculate dy/dx based on the two columns, you can modify the code function like this
function out = processData(x)
out.derivative = diff(x);
out.derivative = out.derivative(:, 2)./out.derivative(:, 1);
out.min = min(out.derivative);
out.max = max(out.derivative);
end
You can find derivative in just one line as I have done in above function.
is there any other way without using function.m file. i mean can i write it in the same code file(if not, what is the reason? to create .m file). and i am using {} bases before. thats why i am unable to run function file.
2. out.derivative = out.derivative(:, 2)./out.derivative(:, 1); if i write this line, Index exceeds matrix dimensions?--- getting this error!
3. Is there any way to bring out max and min values from function. i want to plot it. 'plot(out.max,profilenum)'. all values are under y cell in that derivative 'struct' and min and max values 'struct' are there!
1) You can just place this function at the end of your current script file and it will work, no need to create a new file. Also {} failed because cellfun() require a cell array as input.
2) can you check size(out.derivative). It should be (something) * 2. This error should not be occurring because each cell in | cellvalues| contain (something)*2 element.
3) Get all max values like this
maxValues = [out.max];
it will return an array of max values.
i am very thankfull for answering all myquestion whether its small(stupidity) or big.
1) i tried but matlab says,Function definitions are not permitted in this context. and
if i write without function as below:-
convert = cellvalues();
%y1 = diff((convert(:, 2))./diff(convert(:, 1))); and different ways
tired--- says Undefined function 'diff' for input arguments of type 'cell'.!!
2) its not stored in matalb workspace. but, if we open y struct... in that it shows three rows --diff,max,min. i tried to write in command or script it says undefine.
3) i tried it also, undefine out varible? but no luck for me!!!
You need to create a script file. You cannot just paste a function definition in the command window. Also, don't try to use Evaluate selection option available in the script window. You need to run the whole script (Use Run button, or type name of the script in the command window).
If you want to run the code directly from command window then you will need to create a separate function file, with the function I wrote in my answer. After you correct this issue, other error will probably go away.
Let me summarize the procedure,
  • Create a new function file or add this function to the end of you existing file
function out = processData(x)
out.derivative = diff(x);
out.derivative = out.derivative(:, 2)./out.derivative(:, 1);
out.min = min(out.derivative);
out.max = max(out.derivative);
end
  • If you created a new function file then run the following from command window. If you added it to script file, add the following line to your script above function definition
y = cellfun(@processData, x);
Here x in your cell array and y is the output struct array.
  • plot the max values using
plot([y.max])

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Asked:

Ram
on 30 Apr 2018

Commented:

on 1 May 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!