-1

I have a Matlab array of variable size of zeros and a list of characters in a cell array.

cell array:

{'0000'}
{'0011'}

I want to place -1 in the array for each 0 bit and place 1 for 1 bit.

example:

-1 -1 -1 -1   0  0  0  0   0  0  0  0   0  0  0  0
 0  0  0  0  -1 -1 -1 -1   0  0  0  0   0  0  0  0
 ....
 0  0  0  0   0  0  0  0   0  0  0  0  -1 -1 -1 -1

-1 -1  1  1   0  0  0  0   0  0  0  0   0  0  0  0
 ...
 0  0  0  0   0  0  0  0   0  0  0  0  -1 -1  1  1 

As you can see based on the cell array bits {0000} first four output rows completely place these 4 translated bits in all output rows. Same for the second character {0011}. Array size for this case is (8 by 16). The number of times -1 -1 -1 -1 in the array propagates from left to right is also variable -- for this case, it is up to 4 columns of 4 bits.

In the same way, if there were 5 bits for example:

cell array:

{'00000'}
{'00111'}

This would be the array:

-1 -1 -1 -1 -1   0  0  0  0  0  
 0  0  0  0  0  -1 -1 -1 -1 -1  
-1 -1  1  1  1   0  0  0  0  0 
 0  0  0  0  0  -1 -1  1  1  1 

In this case, the array size is (4 by 10) as the number of times -1 -1 -1 -1 -1 in the array propagates from left to right is up to 2 columns of 5 bits.

The first example gets 4 propagation and the second example has only 2 because the available column number is also a variable. This is a separate parameter.

Here is my code that is not working:

    counterop=0
    counter=1 
     for i=1: numel(listofss)      % list of ss is the list of charecters (my examples has 3) 
        for c = 1:1:(ss)               % row 
            for e = counter:counter+(n-1)     % column
                A_ss(c  ,e)= -1    %A_ss is the predefined matrix (in my example a  12 by 64 matrix )
            end
             counter=counter + n
             counterop=counterop+1
             end
     end   
     if counterop > n-1
         counter = 1
         counterop=1  
    end

A working code with for loop preferably would be appreciated.

  • 3
    This seems terribly unclear. I think you need to spell out in more detail how the examples work. Does "row" always refer to the output array? Or do you somehow see "rows" in the input, too? – tripleee Nov 05 '20 at 05:39
  • 2
    It is not at all clear why the first example gets 4 repetitions, and the second one only 2. Is this a separate parameter? It would be good if you posted your code, it might clarify what the problem is. – Cris Luengo Nov 05 '20 at 05:57
  • 2
    You could post your working code on https://codereview.stackexchange.com/ and ask for recommendations. It would probably help to post it here to. – Thomas Sablik Nov 05 '20 at 06:05
  • Thank you all the comments are addressed. Yes, it's always output rows I am showing. But I know how many rows and columns there will be in the output. And there are 4 repetitions and 2 repetitions in the two examples because this is also a separate parameter. Please help. –  Nov 05 '20 at 06:19
  • I added the faulty code. –  Nov 05 '20 at 15:01
  • For you next question, read [how do I ask a good question](https://stackoverflow.com/help/how-to-ask). Your examples are not complete, your code cannot be run (and is therefor useless) , your explaination are really unclear and the title do not describe your problem. – obchardon Nov 05 '20 at 15:46
  • Okay thank you. Sorry. –  Nov 05 '20 at 16:01
  • Noticed that it is nothing against you, but you won't get any good answer if your question lack of basic informations. – obchardon Nov 05 '20 at 16:05

1 Answers1

1

Here is an example using kron:

np = 4;                         % number of permutations/repetitions
nc = 2;                         % number of cells

x   = {'0000','0011'}           % the cell
M   = sign(cell2mat(x.')-48.5)  % convert string to number: ['01'] -> [-1 1]
M   = kron([eye(np)],M)         % apply the kronecker tensor product
ind = reshape(1:nc*np,nc,np).'; % create an alternated index
M(ind(:),:)                     % index the matrix

For the conversion I'm using a small trick: '0'- 0 = 48 in matlab, since matlab admit implicit casting. So matlab take the corresponding ascii value of the character '0' which is 48.

The result:

M =

  -1  -1  -1  -1   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0  -1  -1  -1  -1   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0  -1  -1  -1  -1   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0  -1  -1  -1  -1
  -1  -1   1   1   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0  -1  -1   1   1   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0  -1  -1   1   1   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0  -1  -1   1   1

To understand how this answer work you will have to read some documentation about : kron, cell2mat, implicit casting, matrix indexing and, if you don't know what it is, about the transpose operator .'

obchardon
  • 10,614
  • 1
  • 17
  • 33
  • Yes.It would be good to use for loops. This seems unreadable for people who are unfamiliar with kron. But thanks a lot. –  Nov 05 '20 at 15:58
  • Just read the linked documentation everything is explained. And again, if your question do not specify to use for loop,... it is hard for us to guess what you have in mind. – obchardon Nov 05 '20 at 16:01
  • I apologize. Please let me know if you could come up with a code with for loop. But thank you for this too. –  Nov 05 '20 at 16:03
  • this works ...but I am still trying to understand this ....lol –  Nov 05 '20 at 21:15