Is there a non-iterative equivalent of this expression in MATLAB?

Expression:

for i=1:n
    X(:,i) = [P{i}(:)];
end

where X is the matrix DxN, and P is an array of cells.

+3
source share
3 answers

If you received an array of cells through mat2cell, you may need to sort the image blocks into columns of the array X. This can be done in one step using the IM2COL command .

%# rearrange the large array so that each column of X
%# corresponds to the 4 pixels of each 2-by-2 block
X = im2col(largeArray,[2 2],'distinct');
+1
source
reshape(cat(3,P{:}),[numel(P{1}) n])

Of course, the above solution is just for fun. I would recommend profiling both solutions and using it only if it has a significant performance advantage.

Service and readability are also very important factors to consider when writing code.

+3
source

, :

P{1} = [ 1 2; 3 4];
P{2} = [ 7 8; 9 10];
P{3} = [ 11 12; 13 14];
X = [P{:}]

X =

     1     2     7     8    11    12
     3     4     9    10    13    14

- reshape(), , .

+1

Source: https://habr.com/ru/post/1796684/


All Articles