How to generate a specific number of values within a range ?
16 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi all!
I'm really wondering how to solve this problem: I have an initial matrix Q (100*60), I extracted the first and second non-zero values in each row using this command line
R = size(Q,1);
First_second_ele = zeros(R,2);
for k = 1:R
tmp = nonzeros(qm(k,:)); %extraction first and second element
idx = 1:min(2,numel(tmp));
First_second_ele(k,idx) = tmp(idx);
end
after this , i got two column vectors. Now i want to see 60 elements between the two extracted values.
for example the first two element are :
4.0000 5.3333
i used :
A=linspace(4.0000,5.3333,60);
the result :
A = 4.0000 4.0226 4.0452 4.0678 4.0904 4.1130 4.1356 4.1582 4.1808 4.2034 4.2260 4.2486 4.2712 4.2938 4.3164 4.3390 4.3616 4.3842 4.4068 4.4294 4.4520 4.4746 4.4972 4.5198 4.5424 4.5650 4.5876 4.6102 4.6328 4.6554 4.6779 4.7005 4.7231 4.7457 4.7683 4.7909 4.8135 4.8361 4.8587 4.8813 4.9039 4.9265 4.9491 4.9717 4.9943 5.0169 5.0395 5.0621 5.0847 5.1073 5.1299 5.1525 5.1751 5.1977 5.2203 5.2429 5.2655 5.2881 5.3107 5.3333
How can i do a loop to get the result of all rows?
2 commentaires
Image Analyst
le 13 Sep 2022
Do you want the 60 values to be linearly spaced, or randomly spaced. Not sure what "the result of all rows" is, but you can access each element in A in a for loop like this
for k = 1 : numel(A)
thisAValue = A(k);
% Now do something with thisAValue.
end
Or do you want to interpolate with interp1 to get values of your other array between the two values and then insert those 60 new values into your existing Q matrix somewhere?
Réponse acceptée
Jan
le 13 Sep 2022
R = size(Q,1);
First_second_ele = zeros(R,2);
Y = zeros(R, 60);
for k = 1:R
tmp = nonzeros(qm(k,:)); %extraction first and second element
idx = 1:min(2,numel(tmp));
First_second_ele(k,idx) = tmp(idx);
if numel(idx) == 2
Y(k, :) = linspace(tmp(1), tmp(2), 60);
end
end
Plus de réponses (1)
David Hill
le 13 Sep 2022
a=sort(rand(10,2),2);
A=[];
for k=1:size(a,1)
A=[A;linspace(a(k,1),a(k,2),60)];
end
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements 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!