Problem in my for loop to find maximum y value
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Ismail Qeshta
 el 11 de Nov. de 2017
  
Hi, my for loop keeps repeating the same third value for the maximum index value. Can anyone please check it for me? the full code is:
for k = 1:3
    Driftt  = sprintf('Drift%d.out', k);
    Reactt  = sprintf('React%d.out', k);
    matDrift = importdata(fullfile(Driftt));
    matReact = importdata(fullfile(Reactt));
    x= matDrift(:,2);
    y= -sum(matReact(:,2:11),2);
    plot(x,y)
    hold on;
    %
    sort = zeros(N,2);
    for i=1:N
        [valy,idx] = max(y);
        sort(i,:) = [x(idx,1),valy];
        hold on;
    end
end
2 comentarios
Respuesta aceptada
  Jan
      
      
 el 11 de Nov. de 2017
        
      Editada: Jan
      
      
 el 11 de Nov. de 2017
  
      The code contains several problems:
1. fullfile(Driftt) does not do anything. Define the directory in addition:
Folder = cd;
...
   matDrift     = importdata(fullfile(Folder, Driftt));
2. Do not use "sort" as name of a variable, because this shadows the important function with the same name. This is not an error directly, but trying to use the function|sort()| afterwards produces unexpected behavior.
3. What is the value of "N"?
Perhaps you want:
    sorted = zeros(N,2);
    for i = 1:N
      [valy, idx] = max(y(i, :));  % or y(:, i)?
      sorted(i,:) = [x(idx,1), valy];
      % hold on;  Completely useless here, remove it
    end
hold set the 'NextPlot' property of the current axes such that following plot commands do not remove existing line objects. But you are nor drawing here.
You could do this without a loop also:
[valy, idx] = max(y, [], 2);  % or max(y, [], 1) ?
sorted      = [x(idx, 1), valy];
Perhaps you have to modify the dimensions for the concatenation.
4. The variable "sort" (renamed to "sorted" in my code) is overwritten. Perhaps you want:
sorted = cell(1, 3);
...
[valy, idx] = max(y, [], 2);  % or max(y, [], 1) ?
sorted{k}   = [x(idx, 1), valy];
8 comentarios
  Ismail Qeshta
 el 12 de Nov. de 2017
				
      Editada: Ismail Qeshta
 el 12 de Nov. de 2017
  
			
		
  Ismail Qeshta
 el 12 de Nov. de 2017
				
      Editada: Ismail Qeshta
 el 12 de Nov. de 2017
  
			
		Más respuestas (1)
  Birdman
      
      
 el 11 de Nov. de 2017
        
      Editada: Birdman
      
      
 el 11 de Nov. de 2017
  
      Because you keep overwriting Driftt and Reactt variables in the first for loop. When k=3, Driftt and Reactt arrays take their last shape and therefore you always see same maximum value.
    Driftt  = sprintf('Drift%d.out', k);
    Reactt  = sprintf('React%d.out', k);
This part causes the problem. At last when k=3, Driftt is equal to Drift3.out file and same for the other. Change this. You might need indexing.
2 comentarios
Ver también
Categorías
				Más información sobre Multirate Signal Processing 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!