Grouping repetead values in cells

5 次查看(过去 30 天)
Hi, I would like to group repeated values in a new matrix with four cells. As an simple example lets say that I have these two arrays (a,B) plus a new one (NewE) which is the matrix to my outputs:
a=[1;2;1;2]
NewB=cell(1,length(a))
B=[560800 x 20] %large numerical matrix where the first column contain the same values as in vector "a"
so I think to get the NewB in this simple form whitout using a counter:
for i = 1:length(B)
for j= 1: length(a)
if B(i,1) == a(j,1)
NewB{j}(end+1,:)=B(i,:);
end
end
end
It is obvious that the algoritm doesnt work but the idea was that.
Thank you for your help in advance,
/Fernando

采纳的回答

Ameer Hamza
Ameer Hamza 2018-5-10
编辑:Ameer Hamza 2018-5-10
If you want to are trying to separate the rows of matrix B on the basis of values in its column 1, then just use splitapply(),
NewB = splitapply(@(x) {x}, B, findgroups(B(:,1)))
NewB will have the same number of cells as the number of unique elements in B(:,1).
Edit: As stated by @Fernando, that the matrix must be divided into different portions according to the grouping of 1 and 2 in column 1. The following code will work in that case:
index = [0; find(diff(B(:,1))); 20];
portionSize = diff(fliplr(index));
dividedData = mat2cell(B, portionSize, size(B, 2));
you don't need to specify a separate a, the method split the matrix on the basis of consecutive portions in column 1.

更多回答(3 个)

Jan
Jan 2018-5-10
% Some test data:
a = [1;2;1;2]
B = rand(560800, 20);
B(:, 1) = randi([1,2], 560800, 1);
% Distribute B over cells of NewB:
NewB = cell(1, length(a))
for k = 1:length(a)
NewB{k} = B(B(:, 1) == a(k), :);
end
While a has repeated values, the resulting NewB contains redundant vectors.

Cathal Cunningham
Cathal Cunningham 2018-5-10
Is the matrix B in this case a cell array or a matrix? The comment ""%large numerical matrix" would suggest that it's a numeric array but that contradicts "the first column contain the same values as in vector "a" ". For the first column in B to match the vector a it would require that B is a cell array with the first column having a structure such that
B{1,1} = [1;2;1;2]
for the script to consider it a match. The problem statement is a little unclear.

Fernando
Fernando 2018-5-10
Hi everybody! ..and thanks for your comments! The answer of Jan comes closer but I forgot to comment that for the matrix B the first column of B(:,1) contains, for this exemple, only two values; 1 and 2 BUT in a sequence (group) of data, i.e:
B(:,1)= [1;1;1;1;1;2;2;2;2;2;1;1;1;1;;2;2;2;2;2;2]
I mean that the third and fourth values/rows in the vector "a" are the second repetition in a measurements series for the first and the second values. In this case I would like to have four cells in the matrix NewB like this:
NewB=[{first_1,:},{first_2,:},{second_1,:},{second_2,:}]
I appreciate very much all your comments Kind regards Nando
  3 个评论
Jan
Jan 2018-5-10
Does this mean, that a is an alternating sequence of 1 and 2 in every case?
The more precise the question is at the beginning, the less time is wasted with given not matching answers.
Fernando
Fernando 2018-5-10
You are right @Jan but I´m working with a large database containing specifically matrices with these two types of "problems" in my code. Your answer help me to solve one of the problems when I dont need to check for a "par" of alternated values like in a. The answer of @Ammer is the complement that I was looking for in order to run my code correctly....and It works now!
Thank you very much for your help!!
regards/Saludos
Fernando

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by