Iterate over vector from result of a for loop

1 次查看(过去 30 天)
I have a vector capacity and I want to iterate over this vector in such a way that that in every iteration 480 is subtracted from the resulted vector for example capacity=[750 350 950 650 600 500 450 700 850 950]
loop 1=capacity-480
result=[270 -130 470 170 120 20 -30 220 370 470]
loop 2=result-480
result={-210 -610 -10 -310 -360 -460 -510 -260 -110 -10]
and so on..meanwhile save every result vector I tried different techniques but cant sort out the desired result e.g
for i=1:3
result(i)=capacity(i)-480
final=[result(i);result(i+1)]
end
but I cant understand how to do this.Any expert can help me out in this please?

采纳的回答

Stephen23
Stephen23 2018-7-20
编辑:Stephen23 2018-7-20
This is MATLAB so there is no need to rely on low-level loops. For MATLAB versions R2016b and later:
capacity - 480*(1:5).'
or for older versions:
bsxfun(@minus,capacity,480*(1:5).')

更多回答(3 个)

Aquatris
Aquatris 2018-7-20
编辑:Aquatris 2018-7-20
Since the results will be a matrix, you should use 2 index;
capacity=[270 -130 470 170 120 20 -30 220 370 470];
result = capacity;
result2 = capacity;
for i=1:3
result(i+1,:)=result(i,:)-480;
result2(i+1,:)=capacity-i*480; % alternative way
end
The result and result2 look like this;
result =
270 -130 470 170 120 20 -30 220 370 470
-210 -610 -10 -310 -360 -460 -510 -260 -110 -10
-690 -1090 -490 -790 -840 -940 -990 -740 -590 -490
-1170 -1570 -970 -1270 -1320 -1420 -1470 -1220 -1070 -970
result2 =
270 -130 470 170 120 20 -30 220 370 470
-210 -610 -10 -310 -360 -460 -510 -260 -110 -10
-690 -1090 -490 -790 -840 -940 -990 -740 -590 -490
-1170 -1570 -970 -1270 -1320 -1420 -1470 -1220 -1070 -970
Here the first row of the result variable is the untouched data. The second row is when you subtract 480, the third row is when you subtract 480 twice from the capacity variable and so on.
Hope I was clear.

Christopher Wallace
Here's one solution:
% Number of iterations
numberOfLoops = 10;
amountToSubtract = 480;
% Create an intial matrix to subtract from
capacityInit = [750 350 950 650 600 500 450 700 850 950];
capacity = repmat(capacityInit, numberOfLoops, 1);
% Iterate over the matrix and subtract the desired amount
final = capacity;
for i=1:numberOfLoops
final(i,:) = final(i,:) - 480*i;
end

Catherine Branter
Catherine Branter 2018-11-21
I have a similar sort of question if anyone is able to help please! trying to loop through a matrix - my model has no problem looping through the bandwidths "bw" but i get an error when looping through the priors
the input of prior works in the format when not using a for loop... but i want the model to run through 3 different types of prior probabilities
prior = [0.427,0.226,0.347];
the error i get is: Error using classreg.learning.classif.FullClassificationModel.processPrior (line 264)
Prior probabilities must be a vector of length 3.
when i print prior i get
priors =
0.3300 0.3300 0.3300
0.4270 0.2260 0.3470
0.2000 0.2000 0.6000
bandwidths = [0.2, 0.5, 1];
priors = [0.33 0.33 0.33; 0.427 0.226 0.347; 0.2 0.2 0.6];
global mymatrix
for f=1:length(bandwidths);
for l=1:length(priors);
bw= bandwidths(f);
p = priors(l)
Mdl = fitcnb(x_train_crossval,y_labels_train_crossval,'ClassNames', class_names,'PredictorNames',predictor_names,'Prior',p,'Width',bw);
  1 个评论
Aquatris
Aquatris 2018-11-22
your for loop is for the variable "priors" but previously you defined "prior" variable. So these two are two different variables. Make sure you are calling the right variable for the "i = 1:length(priors)" part

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by