How to generate a specific number of values within a range ?

13 views (last 30 days)
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
Majid
Majid on 13 Sep 2022
@Image Analyst Hi! the example above is for the first row , i want to do the same for 100 rows, i can't put in each time the two values in "linspace " function.
the result required is new matrix(100,60)
each row contains the 60 elements (not randomly)between the two extracted values as i gave you the first one.

Sign in to comment.

Accepted Answer

Jan
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
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

Community Treasure Hunt

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

Start Hunting!

Translated by