Spica
Spica

Reputation: 127

How to trim a series of arrays in Matlab by eliminating elements

I wonder if there is a way of looping through a number of arrays of different sizes and trimming data from the beginning of each array in order to achieve the same amount of elements in each array?

For instance, if I have:

 A = [4     3     9     8    13]
 B = [15     2     6    11     1    12     8     9    10    13     4]
 C = [2     3    11    12    10     9    15     4    14]

and I want B an C to lose some elements at the beginning, such that they end up being 5 elements in length, just like A, to achieve:

 A = [4     3     9     8    13]
 B = [8     9    10    13     4]
 C = [10     9    15     4    14]

How would I do that?

EDIT/UPDATE:

I have accepted the answer proposed by @excaza, who wrote a nice function called "naivetrim". I saved that function as a .m script and then used it: First I define my three arrays and, as @excaza suggests, called the function:

 [A, B, C] = naivetrim(A, B, C);

Another solution variation that worked for me - based on @Sardar_Usama's answer below (looping it). I liked this as well, because it was a bit more straightforward (with my level, I can follow what is happening in the code)

A = [4     3     9     8    13]
B = [15     2     6    11     1    12     8     9    10    13     4]
C = [2     3    11    12    10     9    15     4    14]

arrays = {A,B,C}

temp = min([numel(A),numel(B), numel(C)]);  %finding the minimum number of elements
% Storing only required elements

for i = 1:size(arrays,2)
    currentarray = arrays{i}
    arrays(i) = {currentarray(end-temp+1:end)}
end

Upvotes: 0

Views: 511

Answers (2)

Sardar Usama
Sardar Usama

Reputation: 19689

If there are not many matrices then it can be done as:

temp = min([numel(A),numel(B), numel(C)]);  %finding the minimum number of elements
% Storing only required elements
A = A(end-temp+1:end);   
B = B(end-temp+1:end);
C = C(end-temp+1:end);

Upvotes: 2

sco1
sco1

Reputation: 12214

A naive looped solution:

function testcode()
% Sample data arrays
A = [4, 3, 9, 8, 13];
B = [15, 2, 6, 11, 1, 12, 8, 9, 10, 13, 4];
C = [2, 3, 11, 12, 10, 9, 15, 4, 14];

[A, B, C] = naivetrim(A, B, C);
end

function varargout = naivetrim(varargin)
% Assumes all inputs are vectors

% Find minumum length
lengths = zeros(1, length(varargin), 'uint32');  % Preallocate
for ii = 1:length(varargin)
    lengths(ii) = length(varargin{ii});
end

% Loop through input arrays and trim any that are longer than the shortest
% input vector
minlength = min(lengths);
varargout = cell(size(varargin));  % Preallocate
for ii = 1:length(varargout)
    if length(varargin{ii}) >= minlength
        varargout{ii} = varargin{ii}(end-minlength+1:end);
    end
end
end

Which returns:

A =

     4     3     9     8    13


B =

     8     9    10    13     4


C =

    10     9    15     4    14

If you have a large number of arrays you may be better off with alternative intermediate storage data types, like cells or structures, which would be "simpler" to assign and iterate through.


Timing code for a few different similar approaches can be found in this Gist.

Performance Profile, MATLAB (R2016b)

Number of Elements in A: 999999
Number of Elements in B: 424242
Number of Elements in C: 101325

Trimming, deletion:        0.012537 s
Trimming, copying:         0.000430 s
Trimming, cellfun copying: 0.000493 s

Upvotes: 3

Related Questions