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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by