how to find a differentiation or derivative of each array(>1000*2) in cell consists 7000*1?
Show older comments
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
Ameer Hamza
on 1 May 2018
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.
Jan
on 1 May 2018
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.
Ram
on 1 May 2018
Ameer Hamza
on 1 May 2018
@Ram, refer to my answer below. It will help you to separate and organize you required variables.
Answers (2)
sloppydisk
on 30 Apr 2018
Should work just fine:
u = cell(5, 1);
u(:) = {rand(20, 2)};
du1 = diff(u{1})
gives me a 19x2 double.
2 Comments
sloppydisk
on 1 May 2018
Please attach the relevant code so we can help you more easily.
Ameer Hamza
on 1 May 2018
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
Ameer Hamza
on 1 May 2018
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.
Ameer Hamza
on 1 May 2018
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.
Ameer Hamza
on 1 May 2018
Edited: Ameer Hamza
on 1 May 2018
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.
Ameer Hamza
on 1 May 2018
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])
Categories
Find more on Environment and Settings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!