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.

7 次查看(过去 30 天)
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 个评论
CodeJunkie
CodeJunkie 2019-1-23
The number of zeros seems to increase based upon length(M)/n terms. for example in my dummy data length(M)/n =25 so my first data set is 25 my second 50 and my 3rd 75. Being PM, PM2 and PM3 respectively. however the only numerical data in all three of these is the last 25 numbers and the rest are zeros. I assume it has some issue to do with how I wrote my loop however I havent been able to find it and as such posed the question.
Jan
Jan 2019-1-23
编辑:Jan 2019-1-23
@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);

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2019-1-23
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 个评论
CodeJunkie
CodeJunkie 2019-1-23
Also I know I've asked a lot but if I were looking to perform further calculations after using mat2cell is there an easy way to do this? so i now have n number for matricies broken down into PM. If i were looking to divide those by a single matrix length n could this be done while PM is in cell form?
Guillaume
Guillaume 2019-1-23
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);

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2019-1-23
编辑:Jan 2019-1-23
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

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by