How do you apply FOR loop to cell arrays?

1 visualización (últimos 30 días)
Abhishek Baba
Abhishek Baba el 24 de Nov. de 2020
Editada: Rik el 24 de Nov. de 2020
I've a large 512x512 matrix which I've divided into 1024 16x16 matrices, and again these 1024 16x16 matrices I've further divided into 4x4 matrix. I've used mat2tiles function for this division.
Then I've applied a function to a 4x4 matrix. Now I want to apply this same function to all the 4x4 matrices present in the cell.
Any idea how to proceed further from this?
Thank you.
  2 comentarios
Rik
Rik el 24 de Nov. de 2020
What have you tried?
You can show exactly what you did either with your actual data, or with data=rand(512,512);.
Abhishek Baba
Abhishek Baba el 24 de Nov. de 2020
Editada: Rik el 24 de Nov. de 2020
function outCell=mat2tiles(inArray,varargin)
tileSizes=[varargin{:}];
N=length(tileSizes);
Nmax=ndims(inArray);
if N<Nmax
tileSizes=[tileSizes,inf(1,Nmax-N)];
elseif N>Nmax
tileSizes=tileSizes(1:Nmax);
end
N=Nmax;
C=cell(1,N);
for ii=1:N %loop over the dimensions
dim=size(inArray,ii);
T=min(dim, tileSizes(ii));
if T~=floor(T) || T<=0
error 'Tile dimension must be a strictly positive integer or Inf'
end
nn=( dim / T );
nnf=floor(nn);
resid=[];
if nnf~=nn
nn=nnf;
resid=dim-T*nn;
end
C{ii}=[ones(1,nn)*T,resid];
end
outCell=mat2cell(inArray,C{:});
This above function converts my 512x512 matrix into 16x16 matrix and again that 16x16 matrix into 4x4 matrix.
My code is:
RGB = imread('Lenna.png'); % Original image loaded
GRAY = rgb2gray(RGB); % Image turned into single channel
Y = double(GRAY);
luma = Y - 128; % 128 is subtracted from each pixel values to produce a data range that is centered around zero
M = mat2tiles(mat2tiles(luma,[4,4]),[4,4]);
(Lenna.png is my 512x512 image)

Iniciar sesión para comentar.

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 24 de Nov. de 2020

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by