How to group values in different indexes (in a matrix) together.

6 vues (au cours des 30 derniers jours)
KibreabG
KibreabG le 8 Nov 2021
Commenté : KibreabG le 8 Nov 2021
I have a 1x4000 matrix of 0s and 1s.
The goal: reformat the matrix so that I group every 3 indexes in this matrix into one index. For example, if my 1x4000 matrix (named myq_stream) has the follwoing data in its first 9 indexes - myq_stream = [1 0 1 0 1 1 1 0 0] - I would want my new matrix (name new_stream) to look like the following - my_stream = [101 011 100].
myq_stream is of type double, so I am not sure how to also maintain the leading zeros in the resulting my_stream. Do I have to change myq_stream to string?
Thank you,
  2 commentaires
the cyclist
the cyclist le 8 Nov 2021
The best way to do this probably depends on what you need to do as the next step. What do you do with the resulting groups?
Maybe just reshaping the array into Nx3 would be useful?
Side note: How do you want to handle the fact that 4000 is not evenly divisible by 3?
KibreabG
KibreabG le 8 Nov 2021
My next step is to count the number of occurrences of each symbol (symbol being the 3-bit digits) within the resulting matrix and calculate the frequency of the symbols. I will then use the frequency to create a 3-bit Huffman code.
For the one remaining bit, I plan to give it two leading zeros and treating it as if it was a complete 1x4002 matrix. Does that make sense?
Also, thank you for your quick response and thoughtful questions.

Connectez-vous pour commenter.

Réponse acceptée

Dave B
Dave B le 8 Nov 2021
Modifié(e) : Dave B le 8 Nov 2021
Yes, if you want 010 to not be 10, it can't be a double...
You didn't describe what should happen with the last triplet, as 4000 is not divisible by 3.
Here are some options for representing this as triplets, I'd recommend considering using a matrix here as that seems like the idiomatic MATLAB approach (depedning on what you're doing with these data):
x=randi(2,1,4000)-1;
x=[x nan nan]; % make it divisible by 3
y1 = reshape(x,3,[])'; % option 1: just treat it as a matrix, when you want triplet n use y(n,:)
y2 = string(y1).join(''); % option 2: make a bunch of strings
y3 = bin2dec(y2(1:end-1)); % option 3: treat the triplets as binary and convert to decimal
  2 commentaires
Dave B
Dave B le 8 Nov 2021
to count frequency of each symbol:
[count,symbol] = histcounts(categorical(y2(1:end-1))); % or clean up your matrix to include the 0 pads
or
[count,s]=histcounts(y3,'BinWidth',1)
symbol=dec2bin(s); %if you want
KibreabG
KibreabG le 8 Nov 2021
Awesome! Thank you very much for giving me different ways I can approach the solution! More code in my arsenal.
Thanks again, I will likely use the y2 option.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by