What this programe do

2 ビュー (過去 30 日間)
licia attouche
licia attouche 2021 年 1 月 16 日
回答済み: Image Analyst 2021 年 1 月 16 日
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 件のコメント
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 日
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 日
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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by