Index and remove values in a cell array

Good evening,
I have a problem with my code for removing the initial values of certain rows of matrices within cell arrays. The problem is that I do not how how to index locations where the conditional of removal is meet, while using a while loop and remove them afterwards, hence the probelm below deleting within the loop.
Could someone help please?
min_sc = min(cellfun(@length,ixv));
a = ixv{size(ixv,2)}(1,1); % get the first column of the final cell value
% Remove inital rows < end cell values.
for k = 1:size(ixv,2)
for v = 1:min_sc
while ixv{k}(v,1)< a
ixv{k}(v,:) = [];
xc{k}(v) = [];
end
while xc_1{k}(v)<a-1 % New while loop because of new condition
xc_1{k}(v) = [];
end
end
end

Respuestas (1)

Hank
Hank el 3 de Dic. de 2019
Editada: Hank el 3 de Dic. de 2019
Its hard to see what the purpose of this deletion is. It looks like you have multiple (499) datasets described by ixv (some kind of index) and by xc or xc_1 (the data). You want to delete values at the begining of the dataset where the index starts after the first index value of the last dataset (a=11).
I tried rewriting your code to be a little more followable. I used the find function to delete in one step instead of using a while loop.
I find that deleting values with a while loop can be problematic because the index of the while loop and the size of the array become out of sync as values are removed. I'm not sure if thats whats happening here though.
Tell me if this does what you're looking for. If not, let me know why, maybe i'll understand you better.
a = ixv{end}(1,1) % first value of last dataset
for k = 1:numel(ixv);
across = find(ixv{k}(:,1)<a,1) % find the first place where the index becomes larger than a.
across_1 = find(xc_1{k})<a-1,1) % same for xc_1 condition
% delete values before across
ixv{k}(1:across-1,:) = [];
xc{k}(1:across-1) = [];
xc_1{k}(1:across_1-1) = [];
end

3 comentarios

Thanks for replying. The code above doesn't work. The purpose of the deletion is to remove values that would cause indices errors in another function because one of the columns would state an index of 0.
If give the previous section of code, it is to determine the y=0 crossing points but because the rows represent nodes in a simulation, some are immediately exposed to changing water level and others are not from a lag period from the face. The problem is when I can come to use the intrepolation function, it cannot index a value at position zero because it doesn't exist (stops at cell 389).
If you look in the ixv variable, the size of the matrices vary from 108x2 to 110x2 i need the initial rows to be removed while that are less than the initial variable in 'a'.
s = PWP_diff(:,1:end-20);
x = 1:size(s,2);
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
% Zero-Crossing Indices Of ‘s’
for i = 1:size(s,1)
xc{i} = zci(s(i,:));
xc_1{i} = xc{i}(:)-1;
ixv{i} = [xc{i}, xc_1{i}];
end
%% Remove initial rows if required
%insrt code here
min_s = min(cellfun(@length,xc)); %Find the min number of elements
ixv_eq = cellfun(@(ixv) ixv(1:min_s,:),ixv,'Uniform',0);
%Index range matrix
for j = 1:size(ixv_eq,2) %No cells = No. nodes
for kk = 1:min_s %No. timesteps
xzc{j}(kk) = interp1(s(ixv_eq{j}(kk,:)), x(ixv_eq{j}(kk,:)), 0, 'spline','extrap'); % Interpolated Zero-Crossings for each node over all timesteps
end
end
Hank
Hank el 4 de Dic. de 2019
Editada: Hank el 4 de Dic. de 2019
I think I just made a parenthesis error. Try this; it worked for me.
a = ixv{end}(1,1); % first value of last dataset
for k = 1:numel(ixv);
across = find(ixv{k}(:,1)<a,1) % find the first place where the index becomes larger than a.
across_1 = find(xc_1{k}<a-1,1) % same for xc_1 condition
% delete values before across
ixv{k}(1:across-1,:) = [];
xc{k}(1:across-1) = [];
xc_1{k}(1:across_1-1) = [];
end
Richard Rees
Richard Rees el 4 de Dic. de 2019
Hi, it is still not doing what it should be doing. Attached is a screen shot showing the ixv{1,287} and {1,409} as an example. The variable a = 11, so these shouldn't be appearing.

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2019b

Preguntada:

el 3 de Dic. de 2019

Comentada:

el 4 de Dic. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by