How can I get multiple values from arrayfun?
    53 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    Chang seok Ma
 am 8 Nov. 2021
  
    
    
    
    
    Beantwortet: Walter Roberson
      
      
 am 8 Nov. 2021
            I have the following sample code.
clear
clc
a = 1:1000;
b = linspace(100,200,1000);     
x1 = arrayfun( @(v,w) fminsearch(@(x) (x(1)-v).^2 + (x(2)-w).^2, [1;1]), a, b, 'UniformOutput', false);
So basically, I would like to get x(1) x(2) that minimizes the objective function given a and b.
and x1 returns 1*1000 cells and each of the cells gives me the minimizer.
However, I want to get 21000 double instead of 11000 cells.
Is there any way I can get x1 as 2*1000?
Thanks in advance.
0 Kommentare
Akzeptierte Antwort
  Stephen23
      
      
 am 8 Nov. 2021
        
      Bearbeitet: Stephen23
      
      
 am 8 Nov. 2021
  
      "However, I want to get 21000 double instead of 11000 cells. Is there any way I can get x1 as 2*1000?"
Of course, just use a comma-separated list to concatenate the cell array contents together:
a = 1:1000;
b = linspace(100,200,1000);
fnh = @(v,w) fminsearch(@(x) (x(1)-v).^2 + (x(2)-w).^2, [1;1]);
x1 = arrayfun(fnh, a, b, 'UniformOutput', false);
x1 = [x1{:}] % comma-separated list
0 Kommentare
Weitere Antworten (2)
  Yusuf Suer Erdem
      
 am 8 Nov. 2021
        clear
clc
a = 1:1000;
b = linspace(100,200,1000);     
x1 = arrayfun( @(v,w) fminsearch(@(x) (x(1)-v).^2 + (x(2)-w).^2, [1;1]), a, b, 'UniformOutput', false);
x2 = reshape(x1,2,500);
Try this way, as you see matrix x2 is the same matrix as x1 but in different dimensions. I hope it helps!
0 Kommentare
  Walter Roberson
      
      
 am 8 Nov. 2021
        a = 1:1000;
b = linspace(100,200,1000);     
x1 = cell2mat(arrayfun( @(v,w) fminsearch(@(x) (x(1)-v).^2 + (x(2)-w).^2, [1;1]), a, b, 'UniformOutput', false));
size(x1)
0 Kommentare
Siehe auch
Kategorien
				Mehr zu Entering Commands finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



