I have a 256×256 matrix from that I want to create block of 8×8 matrices. I have a below code which shows just one block i want all the blocks then subtract a number from each element of the matrix. When i do the subtract 128 from each element it doesn't show negative values rather showing only 0.
[x,y] = size(M);
for i=1:8
for j=1:8
k(i,j) = M(i,j);
end
end
disp(k);
for a=1:size(k)
for b=1:size(k)
A(a,b) = k(a,b) - 128;
end
end
disp(A);
Best Answer
well, if you have to subtract a fixed value it's better to do it as
It's faster.
You said you get
0
values instead of negative ones; it is likely due to the type of the matrix which isunsigned
(i.e. the guys are just positive). A cast to general integer is necessary. Try:To obtain the partition I propose the following, there can be more efficient ways, btw:
matr will contain all the sub-matrices in lexicographical order. e.g.
Explanation: In matlab you can efficiently operate on matrices and slices of them. For instance, you can easily copy submatrices, e.g. the first 8x8 matrix is
You want all the submatrices, thus you need kind of a lattice of indices to get
i.e. you need the lattice
you can use
meshgrid
for that.Finally you have to cut off the pieces, that is what the last two instruction do. particularly the first one generates the indices (try
idx{1}
,...), and the second one generates the submatrices (trymatr{1}
,...).