What is wrong with this

I just want to create a variable to hold 5 different sets of values...
Mass_red
Any ideas??
load remove_outlier.mat
LMT = [20 25 28 32 36];
HMT = [40 40 40 40 39.5];
for k=1:5
i=1;
while(Mass(i) < LMT(k))
i=i+1;
end
j=1;
while(Mass(j) < HMT(k))
j=j+1;
end
Mass_red(k) = Mass(i:j);
end
Thanks
Note: Mass is loaded in to the workspace. It is a 3201 x 1 double. Basically, it is a vector with sequential values from 15 to 47. I suppose you could say it was 15:0.01:47

Answers (4)

z = bsxfun(@times,Mass,bsxfun(@ge,Mass,LMT)&bsxfun(@lt,Mass,HMT));
Mass_red = z(any(z,2),:)';
OR [EDIT]
t = bsxfun(@ge,Mass,LMT)&bsxfun(@le,Mass,HMT);
z = bsxfun(@times,Mass,t)
Mass_red = arrayfun(@(ii)z(t(:,ii),ii),(1:size(z,2)),'un',0);

4 Comments

S
S on 13 Apr 2012
I have no idea what that is but I get an error when I copy it to the command window:
??? Error using ==> bsxfun
Non-singleton dimensions of the two input arrays must
match each other.
S
S on 13 Apr 2012
Actually it works. But it is just padding them all up with zeros to make them the same...
So yes, let me try again. Pretty good. Still dont know what it means :-)
S
S on 13 Apr 2012
I suppose it works but its not what I need. I dont want all these zeros padding up the vector.
Really I want like an array of vectors.
triple bsxfuns!
+1
Thomas
Thomas on 13 Apr 2012
To begin with:: you do not define Mass but you still use it to check its value in a while loop.
what is Mass?
EDIT
I think This is what you need
Mass=15:0.01:47;
LMT = [20 25 28 32 36];
HMT = [40 40 40 40 39.5];
for k=1:5
i=1
if (Mass(i) < LMT(k))
i=i+1
end
j=1
if (Mass(j) < HMT(k))
j=j+1
end
Mass_red(k,:) = Mass(i:j);
end

11 Comments

S
S on 13 Apr 2012
Sorry. My bad.
I missed out a line.
I have amended the original post.
Thanks
abbreviation for the great state of Massachusetts?
S
S on 13 Apr 2012
??? Subscripted assignment dimension mismatch.
Mass_red(k,:) = Mass(i:j);
Thomas
Thomas on 13 Apr 2012
above code works just fine for me.. maybe you have Mass '
after import try Mass=Mass'
S
S on 13 Apr 2012
thanks but no luck.
When I run in debug, it works for the first iteration (k=1), and create Mass_red 1 x 2001 double. But then on the next iteration (k=2), it crashes after implementing the Mass_red(k,:) line of code and gives the error (previous comment).
Thomas
Thomas on 13 Apr 2012
what output does size(Mass) give you? For me Mass is 1x3201 and output Mass_red=5x2001. Also try a 'clear variables' at the beginning before you import your data.. ( though most people hate clearing..)
S
S on 13 Apr 2012
Thanks but neither of the suggestions work.
size(Mass) gives: 3201 1
Thomas
Thomas on 13 Apr 2012
Does copy pasting the above code directly work for you? It does for me..
S
S on 13 Apr 2012
haha yes!
Why is that?? I just copied it in to the command window and it worked.
S
S on 13 Apr 2012
Saying that though... your code is wrong... Mass(i:j) length should change each time since we are cutting it off at different values LMT and HMT
S
S on 13 Apr 2012
It is wrong because... Mass(i) will always be less than LMT(k) and Mass (j) will always be less than HMT(k). Therefore i and j will increment only by 1 for each k.
i:j is going to have varying range depending on the data values, so Mass(i:j) will be varying sizes. You are trying to store each iteration into a numeric array, but the iterations are not of constant size. That is going to be a problem: numeric arrays cannot have rows or columns of varying size. You are going to have to use a cell array:
Mass_red{k} = Mass(i:j);
Notice the {} instead of ()

3 Comments

S
S on 13 Apr 2012
Thanks for advice but:
??? Cell contents assignment to a non-cell array
object.
Mass_red{k} = Mass(i:j);
S
S on 13 Apr 2012
Any advance on this...
Clearly cells are the way forward but I cant assign a non-cell array object to a cell... How can I do this???
That error implies that you initialized Mass_red in some code you did not show us. You need to initialize it differently for cell arrays.
Instead of all that looping over i and j, why not just do
LMTIndex = find(Mass >= LMT(k), 1, 'first');
HMTIndex = find(Mass >= HMT(k), 1, 'first');
startingIndex = min([LMTIndex HMTIndex]);
endingIndex = max([LMTIndex HMTIndex]);
Mass_red{k} = Mass(startingIndex : endingIndex)

This question is closed.

Asked:

S
S
on 13 Apr 2012

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!