How to attribute vectors from a matrix some proprieties?

조회 수: 2 (최근 30일)
Victor Iordache
Victor Iordache 2019년 1월 8일
댓글: Victor Iordache 2019년 1월 12일
I have to create a matrix with 4 columns and with some proprieties.The first column is a ,second b , third c , forth d.Those numbers have to be in an interval ( 1 ,2 ,...,10000) and the conditions are : a>b , (a-c)(b-d)>0, c>d.I've tried to "attach" to a vector a condition that depends on another vector , but i failed,I have to create a matrxi with There are a few rules before creating that matrix:
  1. You can't use while.
  2. You can't use for.
  3. In 1 matrix must appear all of the combinations that verifiy the conditions below.
Does someone know how to apply proprieties to a vector or have any idea how to resolve this problem ? I've tried a few methods but all of them failed.
  댓글 수: 4
Victor Iordache
Victor Iordache 2019년 1월 11일
Luna , here's what i've done so far ,but I have 1 problem .First i'll give you some explanations, a= first column,b-second ,c-third, 4 -forth.The numbers from every column has to be an integer from 1-10000 , numbers from column a has to be > numbers from column b , same applies to c>d and (a-c)(b-d)>0 "example of a matrix of this kind : A=[4 2 2 1]"
I can't solve one thing now,the last property,that (4-2)(2-1)>0.
%n=input('n=')
-i'll change n with 3 in every randi,first i want to create a matrix by 4x4 with this property , if i can manage this i'll just change 3 with n and it will generate a n x 4 matrix.
a=randi([1,10000],1,3);
b=randi([1,10000],1,3);
c=randi([1,10000],1,3)
d=randi([1,10000],1,3)
%first column
x=max(a,b)
y=reshape(x,3,1)
%
%third column
x1=max(c,d);
y1=reshape(x1,3,1)
%
%second column
x2=min(b,d);
y2=reshape(x2,3,1)
%
%forth column
x3=min(c,d);
y3=reshape(x3,3,1)
T=[y,y2,y1,y3]
Victor Iordache
Victor Iordache 2019년 1월 11일
Hello ,Jan.
I have to generate a matrix of n x 4 that has some proprieties.
We'll say that the first column is a , second is b , third is c and forth is d, an example for this kind of matrix is :" A= [ 5 3 2 1]"
The proprieties :
  1. a>b ( 5>3)
  2. c>d (2>1)
  3. (a-c)(b-d)>0 (5-2)(3-1)>0
I need to generate a matrix of n lines with 4 column that has the exact proprieties.All numbers has to be integers from 1 to 10.000.

댓글을 달려면 로그인하십시오.

채택된 답변

Luna
Luna 2019년 1월 12일
Hello Victor,
You can try below code. Please read my comments. I hope it helps.
n = 4;
a = randi(10000,n,1); % creates random a from 1-10000 with n rows
b = arrayfun(@(x) randi(x,1,1),a); % creates random b with n rows. Each element in b is smaller than a's corresponding element (for each row)
% (a-c)(b-d) > 0 means a>c and b>d at the same time. So the following c and d created by this rule
c = arrayfun(@(x) randi(x,1,1),a); % creates random c with n rows. Each element in c is smaller than a's corresponding element (for each row).
d = arrayfun(@(x) randi(x,1,1),b); % creates random d with n rows. Each element in d is smaller than b's corresponding element (for each row).
T = [a,b,c,d]; % your nx4 matrix
%% checks if the below condition is OK for each row.
% if proof is 1, all rows fit the condition.
%If proof is zero at least one row in T does not fit the condition.
proof = any((a-c).*(b-d) > 0);
  댓글 수: 3
Victor Iordache
Victor Iordache 2019년 1월 12일
I've added
n = 4;
a = randi(10000,n,1); % creates random a from 1-10000 with n rows
b = arrayfun(@(x) randi(x,1,1),a); % creates random b with n rows. Each element in b is smaller than a's corresponding element (for each row)
% (a-c)(b-d) > 0 means a>c and b>d at the same time. So the following c and d created by this rule
c = arrayfun(@(x) randi(x,1,1),a); % creates random c with n rows. Each element in c is smaller than a's corresponding element (for each row).
d = arrayfun(@(x) randi(x,1,1),b);% creates random d with n rows. Each element in d is smaller than b's corresponding element (for each row).
e = arrayfun(@(x) randi(x,1,1),c);%creates random e with n rows. Each element in e is smaller than c's
f=min(d,e);
T = [a,b,c,f]% your nx4 matrix
proof = any((a-c).*(b-d) > 0);
is that correct ? I guess ,but i kinda need an approval :))
Victor Iordache
Victor Iordache 2019년 1월 12일
And the last thing, that i really don't know how to do. I have to display every combination of this kind o matrix. I don't know how I should be able to do that , is there a command for this ?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Smoothing and Denoising에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by