Create new matrix with compine values
이전 댓글 표시
Hi everybody I want your help. what I want to do is to have a program to produce a main matrix by using submatrices but by combine some values. For example, I have
a = [0 0 1; 1 0 0; 1 0 1] b = [1 0 1; 1 1 1;1 1 1] c= [0 0 0 ; 1 0 0 ; 0 1 1] d = [ 1 0 1;1 1 1; 1 0 0]
the matrix a has 9 elements which 4 of them are 1 the, b 9 elements with 8 of them are 1, c matrix has 3 elements of 1 and d 6 aces. My question is can I have a new matrix let say S that it has three elements of the above matrixes ( for instance S1 = [a d a] or S2 = [ d d d] or S3 = [ b c b] ) but S it will be accept only if contains from 3 up to 6 aces in average and that means the S1 and S2 matrix is acceptable but S3 in not. S1 - S2 - S3 are examples and might be a different one
댓글 수: 6
Rik
2020년 2월 7일
So you want a random combination of 3 of the 4 matrices, but you have some restrictions about what the result should be. Should that combination be randomly selected? Can you explain what your criteria for rejection are?
Ang Vas
2020년 2월 7일
Rik
2020년 2월 7일
What do you mean with aces?
Ang Vas
2020년 2월 7일
As an engineer I've never heard of that abbreviation. It's not much an abbreviation, since ace and one have as many letters! Anyway,
Why is [c c c] valid when the number of ones sum up to 9 which is clearly greater than 6? In fact, it doesn't appear that any combination of 3 matrices would be valid since the combination with the least ones, [c c c], is already above your threshold.
Ang Vas
2020년 2월 7일
채택된 답변
추가 답변 (1개)
fred ssemwogerere
2020년 2월 7일
Hello, this should do nicely using a 3D-cell array:
a = [0 0 1; 1 0 0; 1 0 1]; b = [1 0 1; 1 1 1;1 1 1];
c= [0 0 0 ; 1 0 0 ; 0 1 1]; d=[ 1 0 1;1 1 1; 1 0 0];
% Store your matrices in a cell array
M={a,b,c,d};
% Compute number of nonzero elements for each matrix and store them in a cell array
N={nnz(a),nnz(b),nnz(c),nnz(d)};
% for a concantenation of 3 matrices use 3 "for" loops
for i=1:size(M,2)
for j=1:size(M,2)
for k=1:size(M,2)
% Computing the mean of any 3 number non-zero elements
P{i,j,k}=mean([N{i},N{j},N{k}]);
% Set a conditional "if" to determine which average of nonzero numbers lies within 3-6 as you want
if 3<=P{i,j,k}&& P{i,j,k}<=6
% Arrays that meet the above condition are horizontally concatenated
L{i,j,k}=horzcat(M{i},M{j},M{k}); %#ok<*SAGROW>
else
% Cell arrays that don't meet the condition are set to zero (they are not required)
L{i,j,k}=0;
end
end
end
end
댓글 수: 1
Guillaume
2020년 2월 7일
I would recommend using numel(M) rather than size(M, 2). That way the code works whether M is a row or column vector (or even a ND matrix).
The code would also be simpler if you used a 3D matrix instead of a cell array.
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!