How to change Elements in Matrix per row depending on their size in reference to a particular Element (without loop)
    5 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Tim
 el 14 de Feb. de 2014
  
    
    
    
    
    Respondida: Jos (10584)
      
      
 el 18 de Feb. de 2014
            I've Data (in the real case much bigger) like this: A =
     0     3     4     5     2     3     1
     2     4     6     2     1     6     8
     1     1     1     3     2     1     8
     4     4     3     3     1     0     0
     2     3     2     2     1     9     0
Not I want to change elements in different rows if they are bigger than a particular number: For example in Row 1,3,4 there should not be an Element bigger then 3 and in Row 2,5 there should not be an Element larger then 2.
So the Result should look like: A =
     0     3     3     3     2     3     1
     2     2     2     2     1     2     2
     1     1     1     3     2     1     3
     3     3     3     3     1     0     0
     2     2     2     2     1     2     0
I tried to solve the Problem like this but it doesnt work:
A(A([1 3 4],:)>=3)=3
A(A([2 5],:)>=2)=2
Does someone have a good solution or does anyone know where my mistake is?
0 comentarios
Respuesta aceptada
  Azzi Abdelmalek
      
      
 el 14 de Feb. de 2014
        
      Editada: Azzi Abdelmalek
      
      
 el 14 de Feb. de 2014
  
      B=A([ 1 3 4],:);
B(B>3)=3
A([1 3 4],:)=B
B=A([ 2 5],:);
B(B>2)=2
A([2 5],:)=B
Más respuestas (2)
  Jos (10584)
      
      
 el 18 de Feb. de 2014
        The easy way:
A([1 3 4],:) = min(A([1 3 4],:),3) ;
A([2 5],:)   = min(A([2 5],:),2) ;
0 comentarios
  Thomas
      
 el 14 de Feb. de 2014
        A laborious way of doing it..
B=[A(1,:);A(3,:);A(4,:)];
B(B >=3)=3;
C=[A(2,:);A(5,:)];
C(C >=2)=2;
Out=[B(1,:);C(1,:);B(2,:);B(3,:);C(2,:)]
Of course there are gonna be better ways of doing this.. ;)
Ver también
Categorías
				Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



