Asked by summyia qamar
on 20 Jul 2018

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?

Answer by Stephen Cobeldick
on 20 Jul 2018

Edited by Stephen Cobeldick
on 20 Jul 2018

Accepted Answer

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).')

Christopher Wallace
on 21 Jul 2018

I like it. This is what I was imagining but couldn't think of.

summyia qamar
on 21 Jul 2018

genius approach.thankyou.. I have matlab R2018a

Sign in to comment.

Answer by Aquatris
on 20 Jul 2018

Edited by Aquatris
on 20 Jul 2018

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.

summyia qamar
on 20 Jul 2018

Yes its very clear thankyou very much for the solution

Sign in to comment.

Answer by Christopher Wallace
on 20 Jul 2018

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

Sign in to comment.

Answer by Catherine Branter
on 21 Nov 2018

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);

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.