Effacer les filtres
Effacer les filtres

Replacing matrix n column values

6 vues (au cours des 30 derniers jours)
Santos García Rosado
Santos García Rosado le 6 Mai 2021
Hello community,
If I want to make every negative value inside a matrix equal to zero I would use the following code:
MyMatrix(MyMatrix<0) = 0
And If i wanted to make only one column negative values zero I'd use this:
n = 5; % The column i want to replace.
MyMatrix( MyMatrix(:,n)<0, n ) = 0
The real issue is that now I have a 50x100 matrix where I need to apply the previous idea (make every negative value equal to zero) but within columns 70 to 80(range). This could be easily done with a loop, but I'm sure there is a way to work around it.
Any ideas?
Thank you,
Santos

Réponse acceptée

James Tursa
James Tursa le 6 Mai 2021
E.g.,
temp = A(:,70:80);
temp(temp<0) = 0;
A(:,70:80) = temp;
  1 commentaire
Santos García Rosado
Santos García Rosado le 7 Mai 2021
That's it! Thank you James!

Connectez-vous pour commenter.

Plus de réponses (1)

Sergey Kasyanov
Sergey Kasyanov le 6 Mai 2021
Hello,
There are some no pretty but working solutions,
n = [70, 80];
%A is MyMatrix
%1
A(A(:).' < 0 &...
(1:numel(A)) > size(A, 1) * (n(1) - 1) &...
(1:numel(A)) <= size(A, 1) * (n(2))) = 0;
%2
A(A < 0 &...
repmat(1:size(A, 2), size(A, 1), 1) >= n(1) &...
repmat(1:size(A, 2), size(A, 1), 1) <= n(2)) = 0;
%3
n = [70:80]
A(:, n) = A(:, n) .* (A(:, n) >= 0);
  1 commentaire
Santos García Rosado
Santos García Rosado le 7 Mai 2021
Thank you Sergey! Check Jame's solution.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by