How to remove values less than a set min within a cell array

Hi everyone, I have multiple cell arrays, contained within are row vectors. What I want to do is remove any value within these rows that is less than a specified one. I have tried doing it in a loop and using cell function but I cannot quite get it to work.
I know using the loop it is logically detecting that the first element is <22 (min_s), but unlike a normal matrix I cannot remove those values.
The cell function I have tried to use but cannot figure out away around the '<' being rejected
load 'cell_test.mat'
min_s = 22
for i = 5
for j = 1:5%size(av_Nod_sqz_select{1,1},1)
[ppks{i}{j,1},plocs{i}{j,1}] = findpeaks(av_Nod_sqz_select{1,i}(j,:));
if plocs{i}{j,1}(:) < min_s
plocs{i}{j,1} = [];
end
end
end
A = cellfun(@(plocs) plocs(:,plocs<min_s), plocs, 'Uniform',0);% One of many attempts

5 Comments

Well, something's really peculiar in your input array...
>> ~cellfun(@isempty,av_Nod_sqz_select2)
ans =
2×5 logical array
0 0 0 0 1
0 0 0 0 1
>>
in that only the last column is not empty; all the first four columns of the array contain nothing.
The two cells that do have something in them are:
>> cell2mat(cellfun(@size,av_Nod_sqz_select2(:,5),'uniform',0))
ans =
5.00 3600.00
1.00 1.00
>>
an array of 5x3600 and one element. The one element looks like it might have been a variable name
>> av_Nod_sqz_select2{2,5}
ans =
1×1 cell array
{'PWP_kPa'}
>>
while the array is a double array
>> data=av_Nod_sqz_select2{1,5};
>> whos data
Name Size Bytes Class Attributes
data 5x3600 144000 double
>>
You can't remove arbitrary locations in an array; but you could flag them w/ missing value indicator like NaN...
But, the big problem here is the data storage isn't what you apparently think it is and it's very convoluted. I'd suggest stepping back to the way in which this was created and making that more nearly fit the needed arrangement so can accomplish the objective instead of fighting addressing issues as now.
Hi, thanks for the reply.
I looked at it afresh today and with your advice in mind, realised that I did overcomplicate it. Thanks again
for i = 1:size(av_Nod_sqz_select,2)
for j = 1:50%size(av_Nod_sqz_select{1,1},1)
[ppks{j,i},plocs{j,i}] = findpeaks(av_Nod_sqz_select{1,i}(j,:));
a = find(plocs{j,i}< min_s);
plocs{j,i}(a) = [];
end
end
OK, well now
>> ~cellfun(@isempty,av_Nod_sqz_select2)
ans =
2×5 logical array
1 1 1 1 1
1 1 1 1 1
>> cell2mat(cellfun(@size,av_Nod_sqz_select2,'uniform',0))
ans =
25.00 3600.00 25.00 3600.00 25.00 3600.00 25.00 3600.00 25.00 3600.00
1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00
>> cellfun(@disp,av_Nod_sqz_select2(2,:))
'Xdisplacement_m'
'Ydisplacement_m'
'XBoundaryForce_kN)'
'YBoundaryForce_kN'
'PWP_kPa'
>>
so there is something in each cell and each in the first row seems to be a given variable over some 2D set of values. That's still pretty complex storage for just doubles as opposed to a 3D arrray, say, where each plane is one of the five variables or a struct with named fields or a table with the array as the data for variable.
av_Nod_sqz_select2{2,3}={replace(av_Nod_sqz_select2{2,3},')','')}; % remove illegal ')' from variable name
tav=cell2table(av_Nod_sqz_select2(1,:),'VariableNames',[av_Nod_sqz_select2{2,:}]); % convert to table
tav =
1×5 table
Xdisplacement_m Ydisplacement_m XBoundaryForce_kN YBoundaryForce_kN PWP_kPa
________________ ________________ _________________ _________________ ________________
[25×3600 double] [25×3600 double] [25×3600 double] [25×3600 double] [25×3600 double]
>>
Then you could just refer to tav.Xdisplacement_m and subscript it as desired and work on it as ordinary 2D array, for example. Or, still can use subscripting expressions to refer to more than one variable at a time if that's the need. But, much easier to visualize and work with than deeply nested cell arrays.
As noted, you could also do something similar with struct and named fields that can also be addressed programmatically.
What is the meaning of the 2D array? Is this a spatial model, maybe?
It's also still not clear what you're really trying to do here?
Describe the desired end result and probably easier way to go at it.
Hi, it is fine. I got what I need it to do with the code above.
Its a 2D geotechnical simulation that I am doing some analysis on regarding pressure changes, the way the data is saved is a seperate spreadsheet per timestep, so I have to collect all the data from that.
Well, there are much simpler ways to go at it...

Sign in to comment.

Answers (0)

Categories

Asked:

on 19 Dec 2019

Commented:

dpb
on 20 Dec 2019

Community Treasure Hunt

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

Start Hunting!