Arrays of unknown size as input
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello everyone,
I want to have an input of 2 arrays which will either be inputed number by number from the user or as a desired range and step. That means that the array sizes won't be known a priori. Thanks a lot in advance!
For clarification:

2 commentaires
Mohammad Sami
le 1 Juin 2020
You have to consider what kind of calculations are you doing on your inputs.
As long as your calculations accept variable length inputs, the dimensions would not matter.
Example if you are doing element wise calculation, the dimensions would not matter.
Réponses (1)
KSSV
le 1 Juin 2020
You need not to worry about the dimensions of input. You can do what you wanted without loop. Check the below code.
[X,Y] = meshgrid(x,y) ;
C = sqrt(X.^2+Y.^2) ;
idx = C == fix(C) ;
pyth = [X(idx) Y(idx)] ;
So if your question is to ask, how to initialize an unknown array. You can do the following.
x=1:100;
y=1:200;
n=length(x);
k=length(y);
count=0;
pyth = NaN([],2) ; % as you know there are two columns
for i=1:n
for j=1:k
c=sqrt(x(i)^2+y(j)^2);
if c==fix(c)
count=count+1;
pyth(count,1)=x(i);
pyth(count,2)=y(j);
end
end
end
2 commentaires
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!