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?
댓글 수: 0
채택된 답변
추가 답변 (3개)
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
2018년 7월 20일
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
댓글 수: 0
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
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!