What this programe do

1 visualizzazione (ultimi 30 giorni)
licia attouche
licia attouche il 16 Gen 2021
Risposto: Image Analyst il 16 Gen 2021
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 Commenti
Ive J
Ive J il 16 Gen 2021
@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 il 16 Gen 2021
@Ive J thanks for the correction! That´s neat!

Accedi per commentare.

Risposte (2)

Image Analyst
Image Analyst il 16 Gen 2021
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 il 16 Gen 2021
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

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by