Reshaping a Char Array

Hi,
I am currently working on a Project where I need to order a Char Array like shown below
A = ['12';'12';'12';'12';'12';'12';'12';'12';'34';'34';'34';'34';'34';'34';'34';'34';'56';'56';'56';'56';'56';'56';'56';'56';'78';'78';'78';'78';'78';'78';'78';'78';'AA';'AA';'AA';'AA';'AA';'AA';'AA';'AA';'BB';'BB';'BB';'BB';'BB';'BB';'BB';'BB';'CC';'CC';'CC';'CC';'CC';'CC';'CC';'CC';'DD';'DD';'DD';'DD';'DD';'DD';'DD';'DD'];
I want to re-arrange the char array like:
'1234'
'1234'
'1234'
'1234'
'1234'
'1234'
'1234'
'1234'
'5678'
'5678'
'5678'
'5678'
'5678'
'5678'
'5678'
'5678'
'AABB'
'AABB'
'AABB'
'AABB'
'AABB'
'AABB'
'AABB'
'AABB'
'CCDD'
'CCDD'
'CCDD'
'CCDD'
'CCDD'
'CCDD'
'CCDD'
'CCDD'
What is the best way to do that? I tried using reshape function but it doesn't seem to work for this case?
Thanks in advance.

 Risposta accettata

Rik
Rik il 16 Ott 2020
This should do it:
A = ['12';'12';'12';'12';'12';'12';'12';'12';'34';'34';'34';'34';'34';'34';'34';'34';'56';'56';'56';'56';'56';'56';'56';'56';'78';'78';'78';'78';'78';'78';'78';'78';'AA';'AA';'AA';'AA';'AA';'AA';'AA';'AA';'BB';'BB';'BB';'BB';'BB';'BB';'BB';'BB';'CC';'CC';'CC';'CC';'CC';'CC';'CC';'CC';'DD';'DD';'DD';'DD';'DD';'DD';'DD';'DD'];
B=mat2cell(A,ones(size(A,1),1),2);
B=reshape(B,8,[]);
part1=B(:,1:2:end);part1=part1(:);
part2=B(:,2:2:end);part2=part2(:);
B=cell2mat([part1 part2]);

1 Commento

Jack Gallahan
Jack Gallahan il 17 Ott 2020
Also is this the most efficient way to do this?
Because I have 134,217,728 Bytes each is 2 chars converting mat2cell and back takes to much time and memory. Sometimes matlab gives Out of Memory error. Is there another way?

Accedi per commentare.

Più risposte (1)

Ameer Hamza
Ameer Hamza il 16 Ott 2020
Something like this
A = ['12';'12';'12';'12';'12';'12';'12';'12';'34';'34';'34';'34';'34';'34';'34';'34';'56';'56';'56';'56';'56';'56';'56';'56';'78';'78';'78';'78';'78';'78';'78';'78';'AA';'AA';'AA';'AA';'AA';'AA';'AA';'AA';'BB';'BB';'BB';'BB';'BB';'BB';'BB';'BB';'CC';'CC';'CC';'CC';'CC';'CC';'CC';'CC';'DD';'DD';'DD';'DD';'DD';'DD';'DD';'DD'];
idx = find([1; diff(A(:,1))]);
B = reshape(string(A), diff(idx(1:2)), []);
B = B(:,1:2:end) + B(:,2:2:end);
B = char(B(:));

6 Commenti

Rik
Rik il 16 Ott 2020
Ah, you must have posted this just before I refreshed the page. Interesting to see a different strategy.
Ameer Hamza
Ameer Hamza il 16 Ott 2020
Interestingly another question today had a somewhat common structure: https://www.mathworks.com/matlabcentral/answers/615958-how-to-create-arrays-from-repeated-matrix-raws#answer_515683?s_tid=prof_contriblnk, so writing this answer just required a bit of modification.
The above numbers was just an example. What if indexes repeated at 256 not 8 like:
A(1) A(257)
A(2) A(258)
A(3) A(259)
. .
. .
. .
. .
A(256) A(512)
A(513) A(769)
. .
. .
. .
. .
A(768) A(1024)
and so on.
Rik
Rik il 16 Ott 2020
Have you seen my answer below? This answer can also be modified for such cases, but mine would have an obvious parameter to edit.
It appears to be working in that case too
A = [repmat('12', 256, 1); repmat('34', 256, 1); repmat('56', 256, 1); repmat('78', 256, 1); repmat('AA', 256, 1); repmat('BB', 256, 1); repmat('CC', 256, 1); repmat('DD', 256, 1)];
idx = find([1; diff(A(:,1))]);
B = reshape(string(A), diff(idx(1:2)), []);
B = B(:,1:2:end) + B(:,2:2:end);
B = char(B(:));
The only case it will fail is if the second letter changes and the first letter remain same, for example
A = ['12';'12';'14';'14';'56';'56';'78';'78';'AA';'AA';'BB';'BB';'CC';'CC';'DD';'DD'];
In that case, following modification
idx = find([1; any(diff(A),2)]);
works, which is a more general form of the line in my answer and work for the original input too.
Jack Gallahan
Jack Gallahan il 17 Ott 2020
Modificato: Jack Gallahan il 17 Ott 2020
The problem is, original data is ADC values which varies continously thus I can not rely on value of first 4 bit and second 4 bit. Values might be '9C', '00','15','0E,'28'..... and so on. The numbers on my first post was just to show the placement of chars that is needed.

Accedi per commentare.

Categorie

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by