Filter löschen
Filter löschen

How to add rows to a mtrix following a certain condition?

1 Ansicht (letzte 30 Tage)
Anderson
Anderson am 26 Apr. 2016
Bearbeitet: Anderson am 28 Apr. 2016
Suppose the following matrix r= [0; 1; 1.5; 1.6; 2; 10.4; 15.5].
How to add rows which are in the interval floor(r):0.1:floor(r)+1?
In the end, the output should be:
r_out = [0; 0.1; 0.2; ...; 1; 1.1; 1.2; ...; 2; 2.1; ...; 3; 10; 10.1; ...; 11; 15; 15.1; 15.2 ; ...; 16]
I have tried the following:
for i=1:size(r,1); p = floor(r(i)):0.1:floor(r(i))+1; q = [r; p']; end
However, it does not cover all the elements. Probably, the loop is erasing previous computation after each update.
  3 Kommentare
Anderson
Anderson am 26 Apr. 2016
Bearbeitet: Anderson am 26 Apr. 2016
No, this is absolutely not a homework!
I have tried the following:
for i=1:size(r,1); p = floor(r(i)):0.1:floor(r(i))+1; q = [r; p']; end
However, it does not cover all the elements. Probably, the loop is erasing previous computation after each update.
Anderson
Anderson am 26 Apr. 2016
I found the problem.
r=unique(r, 'rows', 'stable');
for i=1:size(r,1); p(i,:) = floor(r(i)):0.1:floor(r(i))+1; end
q = unique(reshape(p,[],1));

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Fangjun Jiang
Fangjun Jiang am 26 Apr. 2016
r= [0; 1; 1.5; 1.6; 2; 10.4; 15.5];
rf=unique(floor(r));
r_out=bsxfun(@plus,rf.',(0:0.1:1).');
r_out=r_out(:);
The output is slightly different than what you listed. The difference is the elements of [2:0.1:3]. Not sure which one should be correct based on your post.

Anderson
Anderson am 26 Apr. 2016
I found the problem.
r=unique(r, 'rows', 'stable');
for i=1:size(r,1); p(i,:) = floor(r(i)):0.1:floor(r(i))+1; end
q = unique(reshape(p,[],1));

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by