多维数组查找值

时间:2016-01-20 23:57:06

标签: arrays matlab multidimensional-array

我有以下内容:

A=[2,3,4]

A(:,:,1) =
    0.9572    0    1 
    0    0.1419    0.9157
A(:,:,2) =
    0     0.9575    0
    0.5469    1    0.9706
...

对于矩阵A的每一列,以及每5个切片,我想找到B值介于0和1之间的值。

A(:,:,1)我会得到:

0.9572 0.1419 0.9157 

来自A(:,:,2)我会得到:

0.5469 0.9575 0.9706

最后矩阵B为B(1,3,4)。

有任何建议吗?

感谢

2 个答案:

答案 0 :(得分:0)

我认为这可以满足您的需求:

s = size(A, 3);                         %// number of slices
B = reshape(A, [], s);                  %// collapse first two dimensions into one
ind = (B>0) & (B<1);                    %// logical index of desired values
result = B(ind);                        %// index into B. Gives a column vector
result = reshape(result, 1, [], s);     %// reshape into desired form

答案 1 :(得分:0)

每个切片的值不相同,您必须使用单元格数组:

B = cellfun(@(values) values(0 < values & values < 1),... %// Take values between 0 and 1 for each slice
            squeeze(num2cell(A, [1 2])),...               %// To cell array, each slice into a cell
            'UniformOutput', false);                      %// Output is (and has to be) a cell array
B{1} %// 0.9572 0.1419 0.9157 
B{2} %// 0.5469 0.9575 0.9706