MatLab to transform the matrix relative to the 1st column

This question is the result of MatLab (or any other language) to convert a matrix or csv to put the values ​​of the second column in one row, if the value of the 1st column is the same? and Group values ​​in different rows at the index of the first column

If

A = [2 3 234 ; 2 44 99999; 2 99999 99999; 3 123 99; 3 1232 45; 5 99999 57] 1st column | 2nd column | 3rd column -------------------------------------- 2 3 234 2 44 99999 2 99999 99999 3 123 99 3 1232 45 5 99999 57 

I want to make

 1st col | 2nd col | 3rd col | 4th col | 5th col | 6th col| 7th col -------------------------------------------------------------------- 2 3 234 44 3 123 99 1232 45 5 57 

That is, for each number in the 1st column of A, I want to put the EXCEPT numbers "99999"

If we ignore the part β€œexcept 99999”, we can write the group values ​​in different rows at the index of the first column

 [U, ix, iu] = unique(A(:,1)); vals = reshape(A(:, 2:end).', [], 1); %'// Columnize values subs = reshape(iu(:, ones(size(A, 2) - 1, 1)).', [], 1); %'// Replicate indices r = accumarray(subs, vals, [], @(x){x'}); 

But obviously this code will not ignore 99999.

I assume there are two ways:

 1. first make r, and then remove 99999 2. remove 99999 first, and then make r 

Anyway, I just want faster.

Thank you in advance!

+1
source share
1 answer

I think options 1 is better, i.e. do r first and then delete 99999. Having r, u can delete 99999 as follows:

 r2 = {}; % new cell array without 99999 for i = 1:numel(r) rCell = r{i}; whereIs9999 = rCell == 99999; rCell(whereIs9999) = []; % remove 99999 r2{i} = rCell; end 

Or a more fancy way:

 r2= cellfun(@(c) {c(c~=99999)}, r); 
+1
source

Source: https://habr.com/ru/post/1489475/


All Articles