Borrar filtros
Borrar filtros

Insert array into matrix within a loop

3 visualizaciones (últimos 30 días)
tilfani oussama
tilfani oussama el 30 de En. de 2018
Comentada: Andrei Bobrov el 30 de En. de 2018
I have a matrix X (N,100), i have a function which calculate for each column of X another array h(1,50), a would like to calculate for each column of X(N,:) a vector h and making them all in a matrix H(100,50). Can someone help

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 30 de En. de 2018
Editada: Andrei Bobrov el 30 de En. de 2018
m = size(X,2);
out = zeros(m,50);
for ii = 1:m
out(ii,:) = your_function(X(:,ii));
end
  4 comentarios
tilfani oussama
tilfani oussama el 30 de En. de 2018
yes i did it works Thank you
Andrei Bobrov
Andrei Bobrov el 30 de En. de 2018
Thank you Jos! I am corrected.

Iniciar sesión para comentar.

Más respuestas (1)

Jos (10584)
Jos (10584) el 30 de En. de 2018
Just to make sure: X has 100 columns, You have a function that takes a column and returns a row-vector of 50 columns. And now you want to do this for each column of X and stack all results in a 100-by-50 array. Something like this will help you:
myfun = @(c) [1:50] * mean(c(:)) ;
% just a function that takes a (column) vector and returns a 1-by-50 vector
C = arrayfun(@(k) myfun(X(:,k)), 1:size(X,2), 'un', 0) ; % apply to each column of X
h = cat(1,C{:}) ; % stack each 1-by-50 cell into an array

Categorías

Más información sobre Loops and Conditional Statements 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!

Translated by