reshape matrix in matlab -
i don't see bug anymore...maybe (very :-) ) there's more easier , faster way of doing it... summarized important columns of huge data frame in little expdata (see below).
the problem quite easy, i'm blind easy idea of solving it..
my objective reshape columns b,c,d 1 column expdata afterwards looks expdata2.
i happy, if me out.
my code far:
= [1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5]'; b = [0.3 0.3 0.3 0.3 0.3 0.4 0.4 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.5 0.8 0.8 0.8 0.8 0.8 0.9 0.9 0.9 0.9 0.9]'; c = [0.4 0.4 0.4 0.4 0.4 0.6 0.6 0.6 0.6 0.6 0.8 0.8 0.8 0.8 0.8 0.9 0.9 0.9 0.9 0.9 0.1 0.1 0.1 0.1 0.1]'; d = [0.5 0.5 0.5 0.5 0.5 0.1 0.1 0.1 0.1 0.1 0.7 0.7 0.7 0.7 0.7 0.2 0.2 0.2 0.2 0.2 0.3 0.3 0.3 0.3 0.3]'; e = rand(25,1); f = rand(25,1); a2 = [2 3 4 2 3 4 2 3 4 2 3 4 2 3 4]'; b2 = [0.3 0.4 0.5 0.4 0.6 0.1 0.5 0.8 0.7 0.8 0.9 0.2 0.9 0.1 0.3]'; c2 = rand(15,1); d2 = rand(15,1); expdata = horzcat(a,b,c,d,e,f); expdata2 = horzcat(a2,b2,c2,d2); % explanation of objective k = horzcat(expdata(:,2),expdata(:,3),expdata(:,4))'; % how wanted expdata(:,2:4) = []; k = reshape(k,[],1); index = 1:size(expdata,1) if expdata(index,1) == 1 expdata(index,:) = []; end if expdata(index,1) == 5 expdata(index,:) = []; end end k = k(1:size(expdata,1),:); expdata2 = [expdata k];
your current code throws error, since number of loop iterations gets determined @ beginning of loop. removing rows of expdata
, run out of rows index @ point.
the quick fix start looping back, i.e. use for index = size(expdata,1):-1:1
. way, can safely remove rows without running indexing problems.
the elegant fix use ismember
identify rows remove:
rows2remove = ismember(expdata(:,1),[1 5]); expdate(rows2remove,:) = [];
Comments
Post a Comment