How to generate a specific number of values within a range ?
40 views (last 30 days)
Show older comments
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 Comments
Image Analyst
on 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?
Accepted Answer
Jan
on 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
More Answers (1)
David Hill
on 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 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!