how to solve matrix?

if i have matrix M=[1 0 1; 0 0 1; 1 1 1] and T=5 apply a condition on this matrix
if nnz(M)>= T perform task
and make elseif nnz(M)< T should not leave this condition until i make all elements in nnz(M) matrix ==0 but im not able to hold in this condition

Respuestas (1)

Fabio Freschi
Fabio Freschi el 19 de Sept. de 2019

0 votos

If I understand correctly if nnz(M)< T the matrix is the null matrix
M = [1 0 1; 0 0 1; 1 1 1];
T = 5;
if nnz(M)>= T
fprintf('My task\n')
else % not necessary: elseif nnz(M)< T
M(M ~= 0) = 0;
end

11 comentarios

Ali Mukhtar
Ali Mukhtar el 19 de Sept. de 2019
do you know if we can use goto statment?
Fabio Freschi
Fabio Freschi el 19 de Sept. de 2019
why?
Ali Mukhtar
Ali Mukhtar el 19 de Sept. de 2019
because by using goto statment with if and else i can solve my problem
Fabio Freschi
Fabio Freschi el 19 de Sept. de 2019
Editada: Fabio Freschi el 19 de Sept. de 2019
I think it is not a good choice
In any case you may want to have a look at continue and break
Ali Mukhtar
Ali Mukhtar el 19 de Sept. de 2019
so you recommend not to use goto statment?
Fabio Freschi
Fabio Freschi el 19 de Sept. de 2019
sure! and I am not the only one...
What is wrong with the code I suggested? What do you want to do with matrix M when nnz(M) < T?
Ali Mukhtar
Ali Mukhtar el 19 de Sept. de 2019
im using another if statment in else if for example
M = [1 0 1; 0 0 1; 1 1 1];
T = 5;
if nnz(M)>= T
fprintf('My task\n')
elseif nnz(M)< T
for i=1:m
for j=1:n
if W(i,j)==0.99 % W could be any matrix n=3 & m=3
W(i,j)=1;
elseif W(i,j)<0.99
W(i,j)=0;
end
end
end
if nnz(W)~=0 %i want to repeat this program from for loop
else if
A1=0
A2=0 %could be any switches
end
Guillaume
Guillaume el 21 de Sept. de 2019
I don't know what this talk of goto is about. There are no goto statements in matlab. Very few modern languages have a goto. goto has been proven a bad idea long long ago.
As Fabio wrote in a comment in his answer,
if somecondition
%do something
elseif opposite_of_somecondition
%do something else
end
the opposite_of_somecondition is pointless. So, if you write if nnz(M) >= T, you know that if you enter the else that nnz(M) is <T, there's no point testing again.
if nnz(M) >= T
%some code goes here otherwise the if is pointless
else %no point in checking nnz(M) < T. If we get here, it's guaranteed
%do something else
end
Your double for loop is unnecessary. The same code without a loop:
w(w == 0.99) = 1; %replace 0.99 by 1
w(w < 0.99) = 0; %replace values less than 0.99 by 0
%values greater than 0.99 are unchanged
and if w is guaranteed to never be greater to 0.99 to start with, it's even simpler, just one line:
w = w == 0.99; %set w to 1 when equal to 0.99, set to 0 otherwise
In any case, what you are trying to achieve is very unclear. You need to explain in more details and give us the big picture.
Walter Roberson
Walter Roberson el 21 de Sept. de 2019
It is not strictly true that if you have
if somecondition
%do something
elseif opposite_of_somecondition
%do something else
end
that opposite_of_somecondition will always be true. There are three cases where it is not true:
  1. comparisons involving nan are always false. a<=b being false does not imply a>b if either a or b are nan
  2. if somecondition is empty, then ~somecondition is also empty and neither are considered true
  3. If somecondition is non-scalar, then the condition might hold for some elements but not others, and the negation of the condition might hold for some elements but not others. if only considers the condition to be true if all of the values being tested are non-zero.
Strictly speaking, the empty case falls under the non-scalar case, but I think it is worth mentioning separately.
Walter Roberson
Walter Roberson el 21 de Sept. de 2019
w == 0.99
There might not be any locations in w that are bit-wise identical to the internal representation of 0.99, as 1/10 is not exactly representable in binary floating point, so 0.99 is not exactly representable. There is also round-off difficulties during calculations. You should rarely compare floating point numbers exactly to a literal number that is not an integer (and a good deal of the time, you should not compare exactly to an integer either.)
Guillaume
Guillaume el 21 de Sept. de 2019
Yes, I have purposefully ignored NaNs, empty and vectors in my statement above as the OP is clearly a beginner and nnz cannot return these.

Iniciar sesión para comentar.

Categorías

Más información sobre Sparse Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 19 de Sept. de 2019

Comentada:

el 21 de Sept. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by