Expression values are not inserted into array cell in R - arrays

I have to create n (say 2) no. of matrices of size say 5*5 with different cell value assignment like this :
,,1
-1 0 1 -1 0
1 -1 0 1 -1
0 1 -1 0 1
-1 0 1 -1 0
1 -1 0 1 -1
,,2
0 -1 0 1 -1
1 0 0 1 0
0 0 1 0 1
1 0 -1 0 -1
0 -1 -1 -1 -1
For this I have tried following way:
r = 5
c = 5
a = 2
m = array(2, dim = c(5,5,2))
for(i in 1:dim(m))
{ for (j in 1:dim(m)) {
for (k in 1:dim(m)){
m[i,j,k] = sample(c(-1,1,0),replace =T, 1)
}}}
m
and got following error and warnings :
Error in [<-(*tmp*, i, j, k, value = -1) : subscript out of bounds
In addition: Warning messages: 1: In 1:dim(m) : numerical expression
has 3 elements: only the first used 2: In 1:dim(m) : numerical
expression has 3 elements: only the first used 3: In 1:dim(m) :
numerical expression has 3 elements: only the first used
But when I tried with matrix function it was giving me correct result, then what's wrong with array.
r = 5
c = 5
m = matrix(2,r,c)
for(i in 1:r)
{ for (j in 1:c) {
m[i,j] = sample(c(0,1,-1),replace =T,prob=c(.33,.33,.34),1)
}}
Can anyone please tell me what can be done to resolve this issue.

Related

MATLAB : Obtain a matrix by adding its last lines to the first lines of the basic matrix

I have a matrix B and I would like to obtain a new matrix C from B by adding its last w*a rows to the first w*a rows (w and a will be defined afterwards).
My matrix B is generally defined by :
I would like to obtain matrix C defined in a general way by:
The characteristics of matrices B and C are:
L and w are defined real values;
B0,B1,...,Bw are of dimension: a by b;
B is of dimension: [(L+w)×a] by (L×b);
C is of dimension: (L×a) by (L×b).
Example: For L = 4 and w = 2 I obtain the following matrix B:
The w*a = 2*1 = 2 last rows of B are:
The w*a = 2*1 = 2 first rows of B are:
By adding the two matrices we have:
The matrix C thus obtained is then:
For B0 = [1 0], B1 = [0 1] and B2 = [1 1]. We obtain :
B0, B1 and B2 are of dimension a by b i.e. 1 by 2;
B is of dimension: [(L+w )×(a)] by (L×b) i.e. [(4+2)×1] by (4×2) i.e. 6 by 8;
C is of dimension: (L×a) by (L×b) i.e. (4×1) by (4×2) i.e. 4 by 8.
The matrices B and C that I get are as follows:
B =
1 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0
1 1 0 1 1 0 0 0
0 0 1 1 0 1 1 0
0 0 0 0 1 1 0 1
0 0 0 0 0 0 1 1
C =
1 0 0 0 1 1 0 1
0 1 1 0 0 0 1 1
1 1 0 1 1 0 0 0
0 0 1 1 0 1 1 0
I would like to have some suggestions on how to program this construction so that from a given matrix B I can deduce the matrix C.
Matlab's range indexing should help you do this in a few steps. The key things to remember are that ranges are inclusive, i.e. A[1:3] is a three 3x1 matrix, and that you can use the keyword end to automatically index the end of the matrix row or column.
%% Variables from OP example
w = 2;
L = 4;
B0 = [1 0];
B1 = [0 1];
B2 = [1 1];
[a, b] = size(B0);
% Construct B
BX = [B0;B1;B2]
B = zeros((L+w)*a, L*b);
for ii = 0:L-1
B(ii+1:ii+w+1, ii*b+1:ii*b+b) = BX;
end
%% Construct C <- THIS PART IS THE ANSWER TO THE QUESTION
% Grab first rows of B
B_first = B(1:end-w*a, :) % Indexing starts at first row, continues to w*a rows before the end, and gets all columns
% Grab last rows of B
B_last = B(end-w*a+1:end, :); % Indexing starts at w*a rows before the end, continues to end. Plus one is needed to avoid off by one error.
% Initialize C to be the same as B_first
C = B_first;
% Add B_last to the first rows of C
C(1:w*a, :) = C(1:w*a, :) + B_last;
I get the output
C =
1 0 0 0 0 0 1 1 0 1
0 1 1 0 0 0 0 0 1 1
1 1 0 1 1 0 0 0 0 0
0 0 1 1 0 1 1 0 0 0
0 0 0 0 1 1 0 1 1 0

Create a matrix blockwise attaching blocks like a stairway MATLAB

I want to build an Array containing of different blocks in the following way.
Given the block I want to repeat the block n-times so that it looks like this:
A =
1 0 0 -1 0 0 0 0 0 1 0 0
0 1 0 0 -1 0 0 0 0 0 1 0
0 0 1 0 0 -1 0 0 0 0 0 1
and I want the Array look like this, n times repeating the scheme:
newArray =
1 0 0 -1 0 0 0 0 0 1 0 0
0 1 0 0 -1 0 0 0 0 0 1 0
0 0 1 0 0 -1 0 0 0 0 0 1
1 0 0 -1 0 0 0 0 0 1 0 0
0 1 0 0 -1 0 0 0 0 0 1 0
0 0 1 0 0 -1 0 0 0 0 0 1
and so on...
With the free space being zeros, since the final array should be a sparse array either way.
How can I repeat and attach the block without using loops?
I'm assuming that the leftward offset of each block with respect to a pure block-diagonal matrix is the number of rows of A, as in your example.
You can build a matrix t that 2D-convolved with A gives the result, as follows:
A = [1 2 3 4; 5 6 7 8]; % data matrix
n = 3; % number of repetitions
[r, c] = size(A);
d = c-r;
t = zeros(r*(n-1)+1, d*(n-1)+1);
t(1:(r*(n-1)+1)*d+r:end) = 1;
result = conv2(t,A);
This gives
A =
1 2 3 4
5 6 7 8
result =
1 2 3 4 0 0 0 0
5 6 7 8 0 0 0 0
0 0 1 2 3 4 0 0
0 0 5 6 7 8 0 0
0 0 0 0 1 2 3 4
0 0 0 0 5 6 7 8
Here is a solution using kron:
n = 5; % number of repetitions
v = 3; % overlapping
s = size(A);
B = A(:,1:s(2)-v)
C = zeros(s(1),s(2)-v);
C(:,end-v+1:end) = A(:,end-v+1:end);
result = kron(eye(n) , B);
result(end,end+v)=0;
result(:,v+1:end) = result(:,v+1:end) + kron(eye(n) , C);
When the matrix size is large you can use sparse matrix:
n = 5;
v = 3;
s = size(A);
B = sparse(A(:,1:s(2)-v));
C = sparse(s(1),s(2)-v);
C(:,end-v+1:end) = A(:,end-v+1:end);
result = kron(eye(n) , B);
result(end,end+v) = 0;
result(:,v+1:end) = result(:,v+1:end) + kron(eye(n) , C);

Change values in two columns of a matrix depending on their original values

I have a matrix a,
a=[0 0 0 0 1
0 0 0 1 0
0 0 1 -1 -1
0 1 0 -1 -1
1 -1 -1 -1 -1];
I want to create new matrix b from matrix a, according to the values in columns 2 and 3.
If the second and third columns are 0 0 (as in case of the first two rows) then I want to change those values to 10 9.
If the second and third column are 0 1 (as in the case of the third row) then I want to change those values to 12 9.
So the output would be
b=[0 10 9 0 1
0 10 9 1 0
0 12 9 -1 -1
0 1 0 -1 -1
1 -1 -1 -1 -1];
You can achieve what you want using logical indexing. For example, the first one can be realised as follows:
idx = a(:, 2) == 0 & a(:, 3) == 0;
a(idx, 2) = 10;
a(idx, 3) = 9;
I think you should be able to do the second one yourself.
It may be interesting to have a look at the Getting Started tutorial.

Concatenate binary strings obtained from decimal to binary conversion

I want to place the binary equivalent number for each element side - by side i.e, the final matrix Concatenated_A would be of size m by nbits*n where [m,n] = size(A);
A = [5, 5, 4, 10, 4;
10, 10, 10, 10, 5;
];
I did an attempt but the result is wrong. I need help in correctly implementing the concatenation. Thank you
[m,n] = size(A);
numbits = 4;
for m = 1:M
Abin = dec2bin(A(m,:),numbits);
for j = 1:size(Abin,1)
Concatenated_A(m,:) = Abin(j,:);
end
end
For first row in A(1,:) = 5, 5, 4, 10, 4 ; its decimal conversion of each element would give a matrix as below.
0 1 0 1
0 1 0 1
0 1 0 0
1 0 1 0
0 1 0 0
Then, how can I do something like this :
Concatenated_A(1,:) = [0 1 0 1 0 1 0 1 0 1 0 0 1 0 1 0 0 1 0 0]
The above operation is repeated for every row in A.
You can transpose the result of dec2bin so that the binary representation goes down the columns and then reshape this into the desired shape so that each row is on it's own row. After reshaping, we take the transpose again so that the rows go across the rows again. Also we need to be sure to transpose A before we start so that we encode along the rows.
out = reshape(dec2bin(A.', numbits).', [], size(A, 1)).'
% 01010101010010100100
% 10101010101010100101
Or if you want a logical matrix instead you can compare your character array to the character '1'
out = reshape(dec2bin(A.', numbits).', [], size(A, 1)).' == '1'
% 0 1 0 1 0 1 0 1 0 1 0 0 1 0 1 0 0 1 0 0
% 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0 1

How to change elements of a matrix with reference to a vector of column indices without using for-loop?

I have a matrix
a =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
and b vector
b =
1 2 3 4 5 5
I want to replace value of each row in a matrix with reference value of b matrix value and finally generate a matrix as follows without using for loop.
a_new =
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
0 0 0 0 1
if first element of b, b(1) = 1 so change take first row of a vector and make first element as 1 because b(1) = 1.
How can I implement this without using for loop?
Sure. You only need to build a linear index from b and use it to fill the values in a:
a = zeros(6,5); % original matrix
b = [1 2 3 4 5 5]; % row or column vector with column indices into a
ind = (1:size(a,1)) + (b(:).'-1)*size(a,1); % build linear index
a(ind) = 1; % fill value at those positions
Same as Luis Mendo's answer, but using the dedicated function sub2ind:
a( sub2ind(size(a),(1:numel(b)).',b(:)) ) = 1
Also via the subscript to indices conversion way,
a = zeros(6,5);
b = [1 2 3 4 5 5];
idx = sub2ind(size(a), [1:6], b); % 1:6 just to create the row index per b entry
a(idx) = 1
any of these methods works in Octave:
bsxfun(#eq, [1:5 5]',(1:5))
[1:5 5].' == (1:5)

Resources