How to assign elements in matrix after satisfying certain constraint?
Afficher commentaires plus anciens
How to create a m*n matrix such that the sum of the first (m-1) elements must be less than or equal to the mth element and also the sum of the first (n-1) elements must be less than or equal to the nth element?
For example:
A = [a11, a12, a13, a14; a21, a22, a23, a24; a31, a32, a33, a34]
such that:
a11+a12+a13<=a14
a21+a22+a23<=a24
a31+a32+a33<=a34
a11+a21<=a31
a12+a22<=a32
a13+a23<=a33
a14+a24<=a34
Code for creating matrix A.
4 commentaires
Walter Roberson
le 6 Sep 2018
Perhaps you mean the sum to be with respect to each row or column? In MATLAB with a 2D array, when you talk about first (m-1) elements, then would refer to linear indexing order, which would go a11 a21 a31 a12 a22 a32 a31 a13 a23 a33 a14 a24 a34
Are there range restrictions on the values? Are the values to be chosen randomly subject to the restriction?
What OP wrote as example is that
sum(A(:,1:end-1),2) <= A(:,end)
and
sum(A(1:end-1,:)) <= A(end,:)
which seems pretty clear to me as to the constraints as he wrote explicit indices for the 4x4 example.
The other questions regarding requirements are unstated, indeed, it would be simple enough to just sample a 3x3 and add a delta to the sum() computed therefrom with no other constraints but the ones given.
A=rand(3);
A(:,4)=sum(A,2)+rand(3,1);
A(4,:)=sum(A)+rand(1,4);
>> A
A =
0.8024 0.9445 0.6838 3.4289
0.6696 0.5915 0.3206 1.8515
0.4716 0.2267 0.7489 2.3791
2.6400 1.8580 1.9993 8.5942
>> [all(A(4,:)>=sum(A(1:3,:))) all(A(:,4)>=sum(A(:,1:3),2))]
ans =
logical
1 1
>>
Walter Roberson
le 7 Sep 2018
I would read the constraints as
all( all( cumsum(A(:,1:end-1),2) <= A(:,2:end), 2 ), 1 )
all( all( cumsum(A(1:end-1,:),1) <= A(2:end, :), 1), 2 )
dpb
le 7 Sep 2018
May be, but the internal constraint wasn't what was written...maybe showing alternative interpretations gives OP some of the feedback needed...
Réponses (1)
SYED ABOU ILTAF HUSSAIN
le 7 Sep 2018
0 votes
Catégories
En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!