X=input('Donner une matrice dont les l'element sont ts differnets de zero);
[n,m]=size(X);
for i=1:n
for j=1:m
if X(i,j)<0
M(i,j)=0;
else
M(i,j)=1;
end
end
end
disp(M)
A=M.*X
B=(1-M).*X

댓글 수: 3

Delme
Delme 2021년 1월 16일
Hi Licia
basically the program gives you back two matricies A and B for the matrix X you put in the command window. Matrix A contains the elements in X wich are positive. Matrix B contains the elements of X wich are negative.
For example if you tipe the following input into the command window:
[-3 2 1
4 3 2]
you get A: [0 2 1
4 3 2]
and B [-3 0 0
0 0 0]
Side note: Since the string in line 1 is in french it contains "l'element" you have to delete the " ' ", otherwise the compiler will give you an error.
Ive J
Ive J 2021년 1월 16일
@Valerio Virzi MATLAB is an interpreter not a compiler. And of course you can use that French sentence as input:
X = input('Donner une matrice dont les l''element sont ts differnets de zero')
% OR
X = input("Donner une matrice dont les l'element sont ts differnets de zero")
Delme
Delme 2021년 1월 16일
@Ive J thanks for the correction! That´s neat!

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

답변 (2개)

Image Analyst
Image Analyst 2021년 1월 16일

1 개 추천

The double for loop can be done in a vectorized way in a single line of code instead of 9 lines of code (this is how most MATLAB programmers would do it):
% Ask user for a matrix.
X = input("Donner une matrice dont les l'element sont ts differnets de zero");
M = X >= 0; % Find positive elements.
disp(M)
% Get only the positive elements in A with the negative elements being set to 0.
A=M.*X
% Get only the negative elements in B with the positive elements being set to 0.
B=(1-M).*X
% Alternatively
B = ~M .* X;
The code basically gets the positive and negative elements according to what I said in my comments. Most (I hope) MATLAB programmers would also have put comments into that code, and would have chosen more descriptive variable names so other people, like you for example, could follow the code easier and not be left scratching their head wondering what the code does.
Delme
Delme 2021년 1월 16일

0 개 추천

You can run this example and have a look.
X= [-3 2 1
4 3 2 ];
[n,m]=size(X);
for i=1:n
for j=1:m
if X(i,j)<0
M(i,j)=0;
else
M(i,j)=1;
end
end
end
disp(X)
disp(M)
A=M.*X;
B=(1-M).*X;
disp(A)
disp(B)
This will give you the following:
-3 2 1
4 3 2
0 1 1
1 1 1
0 2 1
4 3 2
-3 0 0
0 0 0

질문:

2021년 1월 16일

답변:

2021년 1월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by