How to create answers to a calculation in an array in a forloop ?

Hello! I'm still fairly new to matlab and have a question to ask.
I have a dataset (labeled "data) and are using two columns of the dataset to do calcuations. The following forloop gets me the answers I am looking for:
a = Data(:,1);
b = Data(:,2);
for l = 1 : length(edges)-1
indexes = a > edges(l) & a <= edges(l+1);
BinSums(l) = sum(b(indexes));
end
Which is a 1 x 16 double with a value in each column!
Now what I want to do, is to do this same calculation 1000 times, so in the end I have an array of 1000 x 16 with each row being a new set of values.
Note: This is only part of a longer and more complicated code that inputs new data for a and b every iteration. i just want to know how I save each iteration into a new array that is 1000 x 16 if that makes sense.
I can provide more detail if needed, thank you so much for the help!!!

 Accepted Answer

Nbins = numel(edges)-1;
BinSums = zeros(1000,Nbins);
for jj = 1:1000
% ...
% different a and b each time
a = Data(:,1);
b = Data(:,2);
% ...
for ii = 1 : Nbins
indexes = a > edges(ii) & a <= edges(ii+1);
BinSums(jj,ii) = sum(b(indexes));
end
end

7 Comments

Hi thanks for the reply! This seems to work however it does seem to save the same answer, 1000 times. Rather than save a new answer or irteration as a new row in the array BinSums?
If a and b don't change each time, then the result in each row of BinSums is going to be the same each time. I guess I would have to see the real code to know what's really going on.
You already have a loop from 1 to 1000, so all you need to do is include that loop index as the row index of BinSums when storing the result.
(There is no need for a second loop from 1 to 1000. The one I showed is replaced by the one you already have.)
clear all
close all
rawdata = xlsread('Initial001.xlsx'); %rawdata set (351 x 87 double array)
%We start off by defining our bin sizes, calling it edges
edges = 2.^(-2:14); %This defines our bins with octave spacing
Nbins = numel(edges)-1;
BinSums = zeros(1000,Nbins);
numpoints=length(rawdata(:,1)); %length of the dataset
for i=1:1000
%First Pick a Value from Column 1 to Use at random
Casts = unique(rawdata(:,1));
numCasts = length(Casts);
IndicesToUse = ceil(rand(numCasts,1)*numCasts);
CastsToUse = Casts(IndicesToUse);
%Next Pick the Images (Column 2) to use at random
for j = 1:numCasts
numImages = 20;
ImagesToUse = ceil(rand(numImages,1)*numImages);
SimData_temp = rawdata(find(rawdata(:,1)==CastsToUse(j)),:);
for k=1:numImages
if k==1 & j==1
SimData = SimData_temp(find(SimData_temp(:,2)==ImagesToUse(k)),:);
else
temp = SimData_temp(find(SimData_temp(:,2)==ImagesToUse(k)),:);
SimData = [SimData;temp]; %this stores the final dataset (228x87 double array)
end
end
end
a = SimData(:,78);
b = SimData(:,80);
for ii = 1 : Nbins
indexes = a > edges(ii) & a <= edges(ii+1);
BinSums(i,ii) = sum(b(indexes));
end
end
Oh okay that makes sense, no need to do 1000 time iteration twice, thank you! I am now seeing an end result to almost what I need except it seems that rows 1-999 are 0's while the last row, 1000, in BinSums are values. Any guesses as to why?
Maybe you're not getting many matches when you do these things:
SimData_temp = rawdata(find(rawdata(:,1)==CastsToUse(j)),:);
SimData = SimData_temp(find(SimData_temp(:,2)==ImagesToUse(k)),:);
temp = SimData_temp(find(SimData_temp(:,2)==ImagesToUse(k)),:);
You can use the debugger to step through the code and make sure it's working the way you intend. Maybe make it do one iteration instead of 1000, get that to work correctly, then switch back to 1000 iterations.
Okay, thank you for all the help!

Sign in to comment.

More Answers (1)

Use discretize to generate the index vector then pass those index / group indices into groupsummary as the grouping variable and your data as the data variable on which to operate.

Asked:

on 29 Jun 2022

Commented:

on 29 Jun 2022

Community Treasure Hunt

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

Start Hunting!