Removal of values from cell arrays

HI I have following arrays:
before={[2,3,4,5,6];[3,4,5,6,7,8];[5,6,7,8]}
after={[5,6];[4,8];5}
I got 'after' array after some calculations which removed some values from 'before' array Now I have another array X(3x7 cell array):
X= {0,1,0,0.9,0.3,[],[]; 0,0.8,0,0,-1,0.6,[]; 1,0.3,0,-0.4,[],[],[]} % [] at end of each cell were added to make it a square matrix.
In X i have got corresponding values for each value in 'before' array. Like 0 is for 2 in before{1,1}. 1 is for 3 in before{1,1}. Similarly for before{2,1} and before{3,1} I have corresponding values in X.
I want to remove those values from X whose corresponding value is removed from 'before'. Like in before{1,1} 2,3,4 are removed so 0,1,0 will be removed from X{1,1} and so on. Final resuly may look like this:
Result= {0.9,0.3; 0.8,0.6; 1,[]}
kindly help

 Accepted Answer

Is it possible for you to rearrange X to be 3x1 array of matrices like before and after? If so then you can use the following:
for rows = 1:size(before,1);
locations = ismember(before{rows},after{rows});
results(rows) = X{rows}(locations==1);
end
If you can't reorganize X it's going to be a bit more difficult, but let me know.

8 Comments

Hi thanks for your time. Sorry i couldn't get rearranging X? Does it mean to Reshape it?
You currently have X as a 3x7 cell array, where each cell contains a 1x1 double. Both before and after are 3x1 cell arrays, where each cell contains a 1xsomething double.
If you can organize your data so that X becomes a 3x1 cell array that would be helpful. I would suggest just doing so in the code here you originally generate X, but if it is not possible to do so there then reshape might be an option.
I try to convert it by replacing the [] in X with zeros. Now it looks like this:
X= {[0,1,0,0.9,0.3,0,0];[0,0.8,0,0,-1,0.6,0];[1,0.3,0,-0.4,0,0,0]}
It gives following error with the code you provided. Kindly help with this
Subscripted assignment dimension mismatch.
results(rows) = X{rows}(locations==1);
You don't want the extra zeros or [] for filler. Because you now have three different matrices you don't need to worry about mismatch sizes, and making them their regular size will solve the mismatching with the locations array.
Yes got it. X is like this now:
X = {[0,1,0,0.9,0.3];[0,0.8,0,0,-1,0.6];[1,0.3,0,-0.4]}
but error is still here:
Subscripted assignment dimension mismatch.
results(rows) = X{rows}(locations==1);
results{rows} = X{rows}(locations)
Solved.. Thanks alot @Jan Simon & @Bob Nbob

Sign in to comment.

More Answers (1)

If all your arrays have consistent dimension, it's a lot simpler. Not very different from the other answer though, just with cellfun,
before={[2,3,4,5,6];[3,4,5,6,7,8];[5,6,7,8];[]}; %added an empty cell
after={[5,6];[4,8];5;[]}; %added an empty cell
%modified X with the same size
X= {[0,1,0,0.9,0.3]; [0,0.8,0,0,-1,0.6]; [1,0.3,0,-0.4];[]}
%now here you get what you want
idx = cellfun(@(x,y) ismember(x,y),before,after,'uni',0);
XX = cellfun(@(x,y) x(y),X,idx,'uni',0)

1 Comment

yes it also resolves problem.. Thank You so much for your time.

Sign in to comment.

Categories

Tags

Asked:

on 15 Mar 2018

Commented:

on 16 Mar 2018

Community Treasure Hunt

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

Start Hunting!