Azzera filtri
Azzera filtri

Can someone do this calculation without for loops ?

2 visualizzazioni (ultimi 30 giorni)
a = [1 2 3; 4 5 6];
b = [ 1 2 3];
for n = 1: size(a,1)
for m = 1:size(a,2)
k(n,m,:)= b.*b*a(n,m)
end
end

Risposta accettata

José-Luis
José-Luis il 15 Giu 2016
Modificato: José-Luis il 15 Giu 2016
k = bsxfun(@times, a , reshape(b.^2,1,1,[]))
alt_k = bsxfun(@times, a , permute(b.^2,[3,1,2]))
  1 Commento
Amelos
Amelos il 15 Giu 2016
this one works also , thanks k = reshape(kron(b.*b,a),[size(a),numel(b.*b)]);

Accedi per commentare.

Più risposte (2)

Joakim Magnusson
Joakim Magnusson il 15 Giu 2016
Do you mean like this?
fun=@(a,b) b.*b*a
k = bsxfun(fun,a,b)

Azzi Abdelmalek
Azzi Abdelmalek il 15 Giu 2016
Modificato: Azzi Abdelmalek il 15 Giu 2016
a = [1 2 3; 4 5 6];
b = [ 1 2 3];
bb=reshape(b.*b,1,1,[])
out=bsxfun(@times,a,bb)

Categorie

Scopri di più su Dynamic System Models in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by