Effacer les filtres
Effacer les filtres

how to vectrorized this code?

2 vues (au cours des 30 derniers jours)
Siddhesh Karbhari
Siddhesh Karbhari le 9 Oct 2017
Modifié(e) : Jan le 9 Oct 2017
close all
clear all
clc
tic;
i=1;
for k=1:1:10;
for a=1:1:10;
nump(i,:) =k*a;
denp(i,:) =[1 a 0];
i=i+1;
end
end

Réponse acceptée

Jan
Jan le 9 Oct 2017
Modifié(e) : Jan le 9 Oct 2017
nump = (1:10) .* (1:10).'; % Auto-Expand >= Matlab R2016b
For older Matlab versions:
nump = bsxfun(@times, (1:10), (1:10).');
denp from KL's solution is perfect already. This is 2 microseconds(!) faster on my computer, but less nice:
denp = zeros(100, 3);
denp(:, 1) = 1;
denp(:, 2) = repmat((1:10)', 10, 1);

Plus de réponses (1)

KL
KL le 9 Oct 2017
nump = cell2mat(arrayfun(@(x) x:x:x*10,1:10,'uni',0))';
denp = [ones(100,1) repmat((1:10)',10,1), zeros(100,1)];
  1 commentaire
Jan
Jan le 9 Oct 2017
+1 for the optimal creation of denp.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by