Iterate over vector from result of a for loop

조회 수: 2 (최근 30일)
summyia qamar
summyia qamar 2018년 7월 20일
댓글: Aquatris 2018년 11월 22일
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).')
  댓글 수: 2
Christopher Wallace
Christopher Wallace 2018년 7월 21일
I like it. This is what I was imagining but couldn't think of.
summyia qamar
summyia qamar 2018년 7월 21일
genius approach.thankyou.. I have matlab R2018a

댓글을 달려면 로그인하십시오.

추가 답변 (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.
  댓글 수: 1
summyia qamar
summyia qamar 2018년 7월 20일
Yes its very clear thankyou very much for the solution

댓글을 달려면 로그인하십시오.


Christopher Wallace
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

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