对每行中的不同列执行条件
Condition execute for different columns in each row
A = [0,0,1,0,1,0,1,0,0,0;
0,0,0,0,1,0,1,0,0,0;
0,0,1,0,1,0,1,0,0,0];
B = [2,5;
1,6;
3,10];
预期输出元胞数组:
C = [1,1,1,1; %// 2-3-4, 3-4-5, 4-5, 5
0,0,1,1,1,0; %// 1-2-3, 2-3-4, 3-4-5, 4-5-6, 5-6, 6
1,1,1,1,1,0,0,0]; %// 3-4-5, 4-5-6, 5-6-7, 7-8-9, 8-9-10, 9-10, 10
矩阵B包括哪些列应该用于执行矩阵A上的条件。例如,B的第一行是2和5;因此应该使用矩阵 A 的第 2 5 列之间的元素来执行条件。 B的第二行是1和6;因此应该使用第 1 列和第 6 列之间的元素来执行条件。等等...
条件:连续3个元素之和大于或等于1则将1写入矩阵C;否则写0。例如A包含0,1,0连续三个元素(和为0+1+0=1),则对矩阵C写1。再如A第二行前三个元素为0 ,0,0(和为0),所以将0写入矩阵C。依此类推...
"Sometimes it can be considered only 1 or 2 successive elements."
比如A的第一行条件执行到第5列结束,那么只考虑第5列的值;这是1。所以1被写入矩阵C。
解释第一行C:
1, since (sum of 2,3,4 elements of A(1,:)) >= 1
1, since (sum of 3,4,5 elements of A(1,:)) >= 1
since max limit is 5, only 2 successive elements are taken here
1, since (sum of 4,5 elements alone of A(1,:)) >= 1
since max limit is 5, only 1 successive element is taken here
1, since (sum of 5th element alone of A(1,:)) >= 1
没有for循环,只有矩阵运算,这么复杂的任务怎么办?或任何技巧?
使用mat2cell
, cellfun
, im2col
and any
subMatLen = 3;
%// Converting both A & B matrix to Cell Arrays to perform operations Row-wise
AC = mat2cell(A,ones(1,size(A,1)),size(A,2));
BC = mat2cell(B,ones(1,size(B,1)),size(B,2));
%// Getting only the columns of each rows within the limits specified by Matrix B
%// Also appended with zeros for my own convenience as it wont affect the 'summing' process
out = cellfun(@(x,y) [x(y(1):y(2)),zeros(1,subMatLen-1)],AC, BC, 'uni', 0);
%// Finally taking each 1x3 sliding sub-matrix and returning 1 if `any` of it is non-zero
%// which is equivalent to summing and checking whether they are >= 1
out = cellfun(@(x) any(im2col(x, [1,subMatLen], 'sliding')), out, 'uni', 0);
您的示例输入:
A = [0,0,1,0,1,0,1,0,0,0;
0,0,0,0,1,0,1,0,0,0;
0,0,1,0,1,0,1,0,0,0];
B = [2,5;
1,6;
3,10];
输出:
>> celldisp(out)
out{1} =
1 1 1 1
out{2} =
0 0 1 1 1 0
out{3} =
1 1 1 1 1 0 0 0
如果您希望它们作为单行或单列矩阵,您可以将其添加到代码的底部:
out = cat(2,out{:})
或
out = (cat(2,out{:})).'
A = [0,0,1,0,1,0,1,0,0,0;
0,0,0,0,1,0,1,0,0,0;
0,0,1,0,1,0,1,0,0,0];
B = [2,5;
1,6;
3,10];
预期输出元胞数组:
C = [1,1,1,1; %// 2-3-4, 3-4-5, 4-5, 5
0,0,1,1,1,0; %// 1-2-3, 2-3-4, 3-4-5, 4-5-6, 5-6, 6
1,1,1,1,1,0,0,0]; %// 3-4-5, 4-5-6, 5-6-7, 7-8-9, 8-9-10, 9-10, 10
矩阵B包括哪些列应该用于执行矩阵A上的条件。例如,B的第一行是2和5;因此应该使用矩阵 A 的第 2 5 列之间的元素来执行条件。 B的第二行是1和6;因此应该使用第 1 列和第 6 列之间的元素来执行条件。等等...
条件:连续3个元素之和大于或等于1则将1写入矩阵C;否则写0。例如A包含0,1,0连续三个元素(和为0+1+0=1),则对矩阵C写1。再如A第二行前三个元素为0 ,0,0(和为0),所以将0写入矩阵C。依此类推...
"Sometimes it can be considered only 1 or 2 successive elements."
比如A的第一行条件执行到第5列结束,那么只考虑第5列的值;这是1。所以1被写入矩阵C。
解释第一行C:
1, since (sum of 2,3,4 elements of A(1,:)) >= 1
1, since (sum of 3,4,5 elements of A(1,:)) >= 1
since max limit is 5, only 2 successive elements are taken here
1, since (sum of 4,5 elements alone of A(1,:)) >= 1since max limit is 5, only 1 successive element is taken here
1, since (sum of 5th element alone of A(1,:)) >= 1
没有for循环,只有矩阵运算,这么复杂的任务怎么办?或任何技巧?
使用mat2cell
, cellfun
, im2col
and any
subMatLen = 3;
%// Converting both A & B matrix to Cell Arrays to perform operations Row-wise
AC = mat2cell(A,ones(1,size(A,1)),size(A,2));
BC = mat2cell(B,ones(1,size(B,1)),size(B,2));
%// Getting only the columns of each rows within the limits specified by Matrix B
%// Also appended with zeros for my own convenience as it wont affect the 'summing' process
out = cellfun(@(x,y) [x(y(1):y(2)),zeros(1,subMatLen-1)],AC, BC, 'uni', 0);
%// Finally taking each 1x3 sliding sub-matrix and returning 1 if `any` of it is non-zero
%// which is equivalent to summing and checking whether they are >= 1
out = cellfun(@(x) any(im2col(x, [1,subMatLen], 'sliding')), out, 'uni', 0);
您的示例输入:
A = [0,0,1,0,1,0,1,0,0,0;
0,0,0,0,1,0,1,0,0,0;
0,0,1,0,1,0,1,0,0,0];
B = [2,5;
1,6;
3,10];
输出:
>> celldisp(out)
out{1} =
1 1 1 1
out{2} =
0 0 1 1 1 0
out{3} =
1 1 1 1 1 0 0 0
如果您希望它们作为单行或单列矩阵,您可以将其添加到代码的底部:
out = cat(2,out{:})
或
out = (cat(2,out{:})).'