I have a cell array (data
) that looks like this (shortened here):
'45.203885' '-90.600123' '119-8001' 733144 NaN
'45.203885' '-90.600123' '119-8001' 733147 NaN
'45.203885' '-90.600123' '119-8001' 733150 NaN
'45.203885' '-90.600123' '119-8001' 733153 NaN
'45.203885' '-90.600123' '119-8001' 733156 NaN
'45.203885' '-90.600123' '119-8001' 733159 NaN
I want to fill in the 5th column of NaN
's depending on whether the 4th column (which are dates that has been converted using datenum
) matches B
.
B
(also a cell) looks like this (also shortened a lot for the example to make sense):
'45.203885' '-90.600123' '119-8001' 733144 '3.3'
'45.203885' '-90.600123' '119-8001' 733150 '9.5'
'45.203885' '-90.600123' '119-8001' 733156 '6.8'
As you can see, the 4th column of dates do not advance consistently in B
. I'm trying to add in NaN
's to column 5 where B(:,3)
and B(:, 4)
do not match data(:,3)
and data(:, 4)
.
The end product should look something like:
'45.203885' '-90.600123' '119-8001' 733144 '3.3'
'45.203885' '-90.600123' '119-8001' 733147 NaN
'45.203885' '-90.600123' '119-8001' 733150 '9.5'
'45.203885' '-90.600123' '119-8001' 733153 NaN
'45.203885' '-90.600123' '119-8001' 733156 '6.8'
'45.203885' '-90.600123' '119-8001' 733159 NaN
If data
was a matrix, I would just do the following:
data_ind = ismember(data(:,3:4),B(:,3:4),'rows');
But I don't know how to do it with a cell. Would some form of cellfun
do the trick?
It's unclear what you're doing with the third column as all of the entries are identical. Also, your question is a bit confusing as to if you want B
in data
or data
in B
. Probably the fastest and most straightforward way to do this is with a for
loop:
data = {'45.203885' '-90.600123' '119-8001' 733144 NaN
'45.203885' '-90.600123' '119-8001' 733147 NaN
'45.203885' '-90.600123' '119-8001' 733150 NaN
'45.203885' '-90.600123' '119-8001' 733153 NaN
'45.203885' '-90.600123' '119-8001' 733156 NaN
'45.203885' '-90.600123' '119-8001' 733159 NaN};
B = {'45.203885' '-90.600123' '119-8001' 733144 '3.3'
'45.203885' '-90.600123' '119-8001' 733150 '9.5'
'45.203885' '-90.600123' '119-8001' 733156 '6.8'};
d3 = data(:,3);
d4 = [data{:,4}].';
for i = 1:size(B,1)
data(strcmp(d3,B{i,3})&d4==B{i,4},5) = B(i,5);
end
Don't be afraid to use for
loops. You can do it with cellfun
too, but it will require the use of eval
.