Search multiple matrices for values at common index positions
显示 更早的评论
I am trying to analyse several tens of 90x90 matrices and I want to find a summary matrix that only shows a logical 1 if an element has any non zero value that is common to all the matrices at the same index position. In the example below the desired result is to show that there is a value in common between all matrices in position [2,2] and [4,4]. I do not wish to show the values themselves or compare their values. I've tried working with ismember , find , intersect , a==b for just two matrices but nothing seems to do the trick. I can't seem to find a similar query in the archives either. Does anyone know of a suitable function or do I need to write an element by element search routine across all the matrices . All matrices are double and all identically sized .
a =
1 0 0 0 0
0 3 0 0 0
0 0 5 0 0
0 0 0 7 8
>> d = [ 0 0 0 12 0; 0 3 0 4 0; 0 0 0 0 0 ; 34 0 0 9 0]
d =
0 0 0 12 0
0 3 0 4 0
0 0 0 0 0
34 0 0 9 0
>> e = [ 0 0 0 0 0; 0 15 0 0 0; 0 0 0 2 0 ; 0 0 0 5 0]
e =
0 0 0 0 0
0 15 0 0 0
0 0 0 2 0
0 0 0 5 0
>> Result=[ 0 0 0 0 0; 0 1 0 0 0; 0 0 0 0 0 ; 0 0 0 1 0]
Result =
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 1 0
6 个评论
James Tursa
2015-6-23
Can you double check your example? The result doesn't seem to match your description.
Azzi Abdelmalek
2015-6-23
In (4,4) the numbers are different
AndyT
2015-6-24
Stephen23
2015-6-24
Lots of separate matrices, which all need to be processed together... this begs the question: why are these separate matrices anyway? All of the fast and neat answers to this question will first concatenate them together before doing whatever processing is required, so why not simply store the data in one large numeric array anyway, instead of in lots of separate variables?
Placing all of the data in one variable will be more robust, prevent variable clutter, and prevents poor programming practices such as dynamically assigning variable names:
Clear planning of how data is organized makes writing programs many times easier, and in this case it is likely that using one array would be better than lots of separate matrices.
AndyT
2015-6-24
Learn to use the dimensions of arrays and MATLAB will fly...
MATLAB is a high-level language, and it works best when data is kept together as much as possible. Avoid nesting, multiple variables and loops (unless you really have to). Then you can use vectorized code to really get the best out of MATLAB: neat and fast operations.
Good luck and have fun!
采纳的回答
更多回答(4 个)
As given in the example:
result = all(cat(3, a, d, e), 3)
Note that if you tens of these matrices, rather than putting them in individual variables, you're better off putting them in a cell array:
allmatrices = {a, b, c, d, e, f, ...};
In which case the above is simply:
result = all(cat(3, allmatrices{:}), 3);
Or even better since they all have the same size, just stack them all up in the third dimension as I have done as a first step, so:
allmatrices = cat(3, a, b, c, d, e, f, ...);
In which case the code is simply:
result = all(allmatrices, 3);
Azzi Abdelmalek
2015-6-23
编辑:Azzi Abdelmalek
2015-6-23
a=[ 1 0 0 0 0 ; 0 3 0 0 0 ; 0 0 5 0 0 ; 0 0 0 7 8]
d=[ 0 0 0 12 0; 0 3 0 4 0; 0 0 0 0 0 ; 34 0 0 9 0]
e=[ 0 0 0 0 0; 0 15 0 0 0; 0 0 0 2 0 ; 0 0 0 5 0]
aa=~~a
dd=~~d
ee=~~e
aa&dd&ee
Martin Brown
2015-6-24
As above, reformat your code to put the matrices into a single 3D matrix, something like:
A(:,:,1) = [1 0; 0 1];
A(:,:,2) = [2 0; 0 0];
find(sum(A(:,:,1:end)~=0,3)==2);
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!