I should also clarify, for PM2 I am getting the right values however instead of being the size I expect it is twice the size and outputs 25 zeros before the answer I seek. I was just curious how I could just return the answers that arent zeros.
Im struggling with why this while loop returns zeros and more or less how to get all the matricies in the while loop to be the same size. Thanks for any and all help in advance. Also M is from an excel document and has 125 values while n is 5.
5 views (last 30 days)
Show older comments
while Uo<=n;
if Uo==1;
i=1:length(M)/n;
PM(i)=M(i);
Uo=Uo+1;
else if Uo==2;
i=1+((Uo-1)*(length(M)/n)):(Uo)*length(M)/n;
PM2(i)=M(i);
Uo=Uo+1;
else if Uo==3;
i=1+((Uo-1)*(length(M)/n)):(Uo)*length(M)/n;
PM3(i)=M(i);
Uo=Uo+1;
else if Uo==4;
i=1+((Uo-1)*(length(M)/n)):(Uo)*length(M)/n;
PM4(i)=M(i);
Uo=Uo+1;
else if Uo==5;
i=1+((Uo-1)*(length(M)/n)):(Uo)*length(M)/n;
PM5(i)=M(i);
Uo=Uo+1;
7 Comments
Jan
on 23 Jan 2019
Edited: Jan
on 23 Jan 2019
@CodeJunkie: Please copy&paste the text to your Matlab editor and press Ctrl-A Ctrl-I for a proper indentation. If you post the code in the forum, either click on the "Code" tool or marke the code an press Ctrl-E for apply a proper formatting. The easier your is to read, the more like is it, that somebody reads and understand it. Thanks.
"current the code outputs the numbers I want but fills each matrix with zeros with the outputted numbers." - What does this mean? Which numbers are correct? If if they are correct, where do the zeros occur?
Or course, if i is e.g. 8:10, calling:
PM9(i)=M(i);
will set the first 7 elements of PM9 to zero. Maybe you want:
PM9 = M(i);
Accepted Answer
Guillaume
on 23 Jan 2019
The while loop is pointless. Since on each iteration it just take the next if branch, you may as well just have a linear code.
More importantly, your code is designed to create numbered variable. I particularly like the fact that if more numbered variables are required, the user is supposed to do the job of the computer and duplicate the code even more. Never create numbered variables, it's a clear indication that your algorithm is broken. There's a simple built-in system in matlab to store numbered things: the indices of matrices (if the things are all the same size) or cell arrays (if not).
As far as I can tell, the whole code can be simplified to:
PM = cell(n, 1); %create cell array to receive all the vectors
for iter 1:n
indices = 1 + (iter-1)*length(M)/n : iter*length(M)/n;
PM{iter} = M(indices);
end
However, it looks like the whole point of the algorithm is to break a vector into n smaller vectors (it would have been a good idea to tell us that). That can be easily achieved without a loop:
PM = mat2cell(M, 1, ones(1, n) * numel(M) / n); %assuming M is a row vector. Otherwise swap the last 2 arguments
With regards to: "The number of zeros seems to increase based upon length(M)/n term". Yes, of course,simply due to the way you assign to your numbered PMs. For example, if length(M)/n+1 is 10, then you're in effect doing:
PM(1:10) = M(1:10)
PM2(11:20) = M(11:20); %this of course creates PM1(1:10) filled with 0
PM3(21:30) = M(21:30) %this of course creates PM2(1:20) filled with 0
etc.
If you'd done
PMx = M(i);
instead, you wouldn't have had that problem. However, as said, do not number variables and use mat2cell instead.
3 Comments
Guillaume
on 23 Jan 2019
Yes, I was going to comment on the whole concept and then forgot. Depending on what you want to do afterward, it could very well be that the best course of action is not to split your vector at all, keeping it as it is and simply indexing it (with (1:length(M)/n) + (desiredsection-1)*length(M)/n) whenever you need a particular section.
Another option is to reshape the vector into a matrix with n row (or if efficient is critical n columns). That would be easier to use than a cell array and allows you to work efficiently on each portion. E.g if you wanted the mean of each section:
PM = reshape(M, n, []).'; %reshape into n columns and transpose. Subvectors are rows of PM
PMmean = mean(PM, 2); %mean of each subvector
PMnormalised = PM ./ PMmean; %dvide each subvector by its mean
Or you can work with cell array. You would have to use a loop or cellfun to apply the same function to each subvector. The equivalent of the above code with a cell array:
PM = mat2cell(M, 1, ones(1, n) * numel(M) / n);
PMmean = cellfun(@mean, PM);
PMnormalised = cellfun(@rdivide, PM, num2cell(PMmean), 'UniformOutput', false);
More Answers (1)
Jan
on 23 Jan 2019
Edited: Jan
on 23 Jan 2019
I assume your code will be much clearer with a for loop and a switch block:
L = length(M);
for Uo = 1:n
switch Uo
case 1
PM = M(1:L/n);
case 2
PM2 = M(1+((Uo-1)*L/n):Uo*L/n);
... etc
end
end
But now you can see, that the cases 2 to 10 use exactly the same code. Then it would be much smarter to assign a cell array as PM{Uo} instead of separate variables.
L = length(M);
PM = cell(1, n);
for Uo = 1:10
PM{Uo} = M(1+((Uo-1)*L/n):Uo*L/n);
end
0 Comments
See Also
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!