Find empty cell in cell array and replace with closest cell
4 views (last 30 days)
Show older comments
Hi,
I have a cell array with 12 cells. The purpose of this array is that it takes in a large data set, and breaks the data up into 30 degree segments from 0 to 360. However, each cell has multiple columns in it.
Sometimes the data does not have any values in a 30 degree array. For example there may be no data between 60 and 90 degrees. What is the best way to find an empty cell, and replace it with the data to the left or the right of the cell. Also, I want the data that is replaced in the empty cell to be replaced with the cell to the left or right for the dataset that has the largest value in column 5, which is listing distances. Also, sometimes there may be two or three empty cells in a row, so i need to figure out how to find the next real cell on either side of an empty one.
This is the code I have so far, however it only looks 1 cells left or right, and currently does not differentiate which cell to the left or right has the largest value for column 5. Also note that my code below is written to sort the data in each cell based off of the value in column 5 from largest to smallest.
for i=1:length(subRelativeTurbLoc) if isempty(subRelativeTurbLoc{i}) && isempty(subRelativeTurbLoc{i+1}) && isempty(subRelativeTurbLoc{i+2}) && isreal(subRelativeTurbLoc{i-1}) sortedTurbines{i} = sortrows(subRelativeTurbLoc{i-1},-5);
elseif isempty(subRelativeTurbLoc{i}) && isempty(subRelativeTurbLoc{i-1}) && isempty(subRelativeTurbLoc{i-2}) && isreal(subRelativeTurbLoc{i+1})
sortedTurbines{i} = sortrows(subRelativeTurbLoc{i+1},-5);
elseif isempty(subRelativeTurbLoc{i}) && isempty(subRelativeTurbLoc{i+1}) && isreal(subRelativeTurbLoc{i+1})
sortedTurbines{i} = sortrows(subRelativeTurbLoc{i+1},-5);
elseif isempty(subRelativeTurbLoc{i}) && isempty(subRelativeTurbLoc{i-1}) && isreal(subRelativeTurbLoc{i+1})
sortedTurbines{i} = sortrows(subRelativeTurbLoc{i+1},-5);
else
sortedTurbines{i} = sortrows(subRelativeTurbLoc{i},-5);
end
end
0 Comments
Answers (1)
Guillaume
on 23 Jan 2015
Edited: Guillaume
on 23 Jan 2015
How about:
c = {[1 2 3 4 1;11 12 13 14 15], [], [21 22 23 24 2;31 32 33 34 6], [], [], [], [41 42 43 44 7; 51 52 53 54 22], [61 62 63 64 8; 71 72 73 74 12], []} %cell array for demo
%c{2} should receive c{1}
%c{4}, c{5}, c{6} should receive c{7}
%c{9} should receive c{8}
cc = [{[0 0 0 0 -Inf]} c {[0 0 0 0 -Inf]}]; %pad input cell array so edges are never empty
iec = find(cellfun(@isempty, cc)); %find indices of empties
before = arrayfun(@(i) max(setdiff(1:i, iec)), iec); %indices of 1st valid set before empties
after = arrayfun(@(i) min(setdiff(i:numel(cc), iec)), iec); %indices of 1st valid set after empties
[~, imax] = arrayfun(@(b, a) max([max(cc{b}(:, 5)) max(cc{a}(:, 5))]), before, after); %imax is 1 to replace with before, 2 with after
ba = [before; after];
replacement = ba(sub2ind(size(ba), imax, 1:numel(iec))); %index of replacement
cc(iec) = cc(replacement);
newc = cc(2:end-1) %remove padding
assert(isequal(newc, c([1 1 3 7 7 7 7 8 8])))
0 Comments
See Also
Categories
Find more on Solar Power in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!