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.
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 .'