Clear Filters
Clear Filters

How to manipulate arrays inside a cell array?

3 views (last 30 days)
Hi,
I have a cell array(1x316) which contains 316 double numeric arrays of various sizes like
{74845x2 double}, {81032x2 double}, {87351x2 double},.....
sample of one array inside the cell-array :
{74845x2}=
0.194 0.0043
0.116 0.0040
0.118 0.0041
0 0.0044
0 0.0037
0 0.0042
0.045 0.0041
0.151 0.0043
0
0.231 0.0040
0
.....................
.....................
Now, Steps to be taken: 1)
Check if the first column values are non-zero,if it is true then, find difference of elements in first and second column separately.
EDIT
There are two cases here:
case i)If there are more than one non-zero elements together in first column then,
find the maximum and minimum values in the group and compute difference as (max-min).
d11 = 0.194(max among 0.118, 0.116, 0.194)- 0.116(Min value of those)
d12 = 0.0043(max among 0.0041, 0.0040, 0.0043)- 0.0040(min of those)
d21 = 0.151-0.045 d22 = 0.0043-0.0041
case ii)If there is only one non-zero element in the first column, no need to take difference.The output should be same.
d31 = 0.231 d32 = 0.0040
2)At last, remove all the zero values from all arrays in the cell.
This way, i need to compute differences among non-zero elements from all the 316 arrays in the cell array and save the results in a new cell array of original size 1x316.
Please help me out on this.
Thanks in advance!
  4 Comments
Stephen23
Stephen23 on 14 Apr 2016
Ah, you are taking the min and max of each group of non-zero values. Is this correct?
Jung BC
Jung BC on 14 Apr 2016
Yes, thats correct.What is making me tough here is, I need to compute differences in each group between 0's.I can't take difference at once removing all 0's here.
e.g.
d11 = 0.194-0.116 d12 = 0.0043-0.0040
d21 = 0.231-0.045 d22 = 0.0043-0.0040
d31,d32; d41,d42 , all values are calculated step by step whenever it is non-zeros.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 14 Apr 2016
Edited: Stephen23 on 14 Apr 2016
Perhaps you should try something like this:
X = cellfun(@(m)m(:,1)>0,C,'UniformOutput',false);
D = cellfun(@(m,x)abs(diff(m(x,:),1,1)),C,X,'UniformOutput',false);
(It could be done in one cellfun call, but this clearly separates the logical condition and the diff operation).
EDIT to place the values into groups (based on non-zero values in the first column), and then calculate the max and min of each group:
M = [0.194,0.0043;0.116,0.0040;0,0.0044;0,0.0037;0,0.0042;0.045,0.0041;0.151,0.0043;0.231,0.0040;0.106,0.0043;0,0.0040];
C = {M};
% Preallocate an output cell array:
D = cell(numel(C),2);
for k = 1:numel(C)
% locate non-zero values in first column:
idx = 0<C{k}(:,1);
% number those values according to each group:
idg = cumsum(0<diff([false;idx]));
% get max and min of each group, for 1st and 2nd column:
mx1 = accumarray(idg(idx),C{k}(idx,1),[],@max);
mx2 = accumarray(idg(idx),C{k}(idx,2),[],@max);
mn1 = accumarray(idg(idx),C{k}(idx,1),[],@min);
mn2 = accumarray(idg(idx),C{k}(idx,2),[],@min);
% calculate difference of max and min:
D{k,1} = mx1-mn1;
D{k,2} = mx2-mn2;
end
  12 Comments
Jung BC
Jung BC on 15 Apr 2016
Many many thanks to you! I appreciate your great support!It worked !
Stephen23
Stephen23 on 15 Apr 2016
My pleasure! It must be my lucky day, having the same question accepted twice :)

Sign in to comment.

More Answers (1)

Jos (10584)
Jos (10584) on 14 Apr 2016
Write a function that does this for a single element of the cell array and then use cell fun If you can write the function as an inline function that will be nice, otherwise write an m-file to do this. As an example
fh = @(x) abs(x(:,1) - x((:,2)) % difference between largest and smallest
OUT = cellfun(fh, YourCellArray)

Categories

Find more on Creating and Concatenating Matrices 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!