Please help...

4 views (last 30 days)
andy ganteng
andy ganteng on 8 Nov 2011
I have function like this,
function knotident(x,nknot,rand)
clc;
[~,n]=size(x);
for i=1:n
z=x(:,i);
c=min(z);
d=max(z);
if nknot == 1
t=random('unif',c,d,[rand,1]);
else
for g=1:nknot
r=random('unif',c,d,[rand,1]);
t(:,g)=r;
end
t=sort(t,2);
end
kgab(:,i)=t;
end
rand and nknot can be any number. now, i wanna save the result from the loop above for t in matrix with size (rand,nknot*n), but i don't know how to do that. in that syntax i use kgab(:,i)=t; but it cant work if nknot>1. Please help me...
thanks in advance
  3 Comments
andy ganteng
andy ganteng on 8 Nov 2011
aim sorry....i've edit my question...now, please help...
andy ganteng
andy ganteng on 8 Nov 2011
if nknot>1 then matlab said
??? Subscripted assignment dimension mismatch.
Error in ==> knotident at 20
kgab(:,i)=t;

Sign in to comment.

Accepted Answer

Jan
Jan on 8 Nov 2011
"kgab(:,i)=t;" does not work if t is a matrix.
I've clean up the source a little bit, most of all the repeated computations are move out of the loop:
function knotident(x, nknot, r)
n = size(x, 2);
dim = [r, nknot];
minx = min(x, 1);
maxx = max(x, 1);
kgab = [r, n * nknot]; % Pre-allocate!
if nknot == 1
for i = 1:n
kgab(:, i) = random('unif', minx(i), maxx(i), dim);
end
else % knot > 1
v = 1;
for i = 1:n
t = sort(random('unif', minx(i), maxx(i), dim), 2);
kgab(:, v:(v + nknot - 1)) = t;
v = v + nknot;
end
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!