开发者

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

开发者 https://www.devze.com 2023-02-15 08:03 出处:网络
The e开发者_开发百科xpression is: for i=1:n X(:,i) = [P{i}(:)]; end where X is a DxN matrix and P is a cell-array.reshape(cat(3,P{:}),[numel(P{1}) n])

The e开发者_开发百科xpression is:

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

where X is a DxN matrix and P is a cell-array.


reshape(cat(3,P{:}),[numel(P{1}) n])

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

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


If you obtained the cell array via mat2cell, you may be wanting to arrange blocks of an image into the columns of an array X. This can be achieved in a single step using the command IM2COL

%# 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');


You might be able to get away with:

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

Then some sort of reshape() to get to where you want to be.

0

精彩评论

暂无评论...
验证码 换一张
取 消