Reforming index without using loops
이전 댓글 표시
Heya,
I've just started using Matlab and need some help on reforming an index based on a logical equation to dictate what data goes into each column without looping. afterwards determine the averaqge of each column.
The dataset is a fixed 1 row array that has been generated beforehand.
Currently the best code I can imagine is basically doing the loop manually which is very ugly
Is there a better way to execute this concept without copy pasting the same code in place of a loop
Heres an example code with my best representation of my issue
% Create a repeating counter with values 1-12 repeating 4 times (48 values total)
counter = 1:12;
counter = repmat(counter,1,4)
% Creating a binary sequence with the same length
binarySequence = randi([0,1],1,length(counter));
% Creating a data set (Fixed)
dataSet = randi(50,1,length(counter));
% Reforming the dataset by taking values of the counter to determine what
% value will go in each column
reformDataSet(:,1) = dataSet(counter == 1);
reformDataSet(:,2) = dataSet(counter == 2);
reformDataSet(:,3) = dataSet(counter == 3);
reformDataSet(:,4) = dataSet(counter == 4);
reformDataSet(:,5) = dataSet(counter == 5);
reformDataSet(:,6) = dataSet(counter == 6);
reformDataSet(:,7) = dataSet(counter == 7);
reformDataSet(:,8) = dataSet(counter == 8);
reformDataSet(:,9) = dataSet(counter == 9);
reformDataSet(:,10) = dataSet(counter == 10);
reformDataSet(:,11) = dataSet(counter == 11);
reformDataSet(:,12) = dataSet(counter == 12)
% Find Average of each column
avgDataSet = mean(reformDataSet)
% Removing data based on when the binary sequence is equal to 0
dataSet(find(binarySequence == 0)) = NaN
% Reforming the dataset by taking values of the counter to determine what
% value will go in each column
binnedDataSet(:,1) = dataSet(counter == 1);
binnedDataSet(:,2) = dataSet(counter == 2);
binnedDataSet(:,3) = dataSet(counter == 3);
binnedDataSet(:,4) = dataSet(counter == 4);
binnedDataSet(:,5) = dataSet(counter == 5);
binnedDataSet(:,6) = dataSet(counter == 6);
binnedDataSet(:,7) = dataSet(counter == 7);
binnedDataSet(:,8) = dataSet(counter == 8);
binnedDataSet(:,9) = dataSet(counter == 9);
binnedDataSet(:,10) = dataSet(counter == 10);
binnedDataSet(:,11) = dataSet(counter == 11);
binnedDataSet(:,12) = dataSet(counter == 12)
% Find Average of each column
avgBinnedDataSet = nanmean(binnedDataSet)
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Communications Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!