Info

This question is closed. Reopen it to edit or answer.

problem with polyfitn and for loop

2 views (last 30 days)
mohamed gryaa
mohamed gryaa on 11 Sep 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
hi, i need help with this problem, i use a for loop to calculate polyfitn,coefficients and R2 like this:
Loudness=[2.79;3.16;3.71;2.29;2.49;2.64;2.9;2.79;2.91;3.35];
FlucStr=[0.0256;0.0277;0.0311;0.0246;0.021;0.0199;0.0194;0.0256;0.0213;0.0208];
Roughness=[0.491;0.6;0.728;0.34;0.425;0.515;0.617;0.491;0.389;0.438];
Sharpness=[1.03;1.11;1.21;0.887;0.934;0.954;0.985;1.03;1.04;1.12];
Leq=[39.7;40.9;42.6;38.1;38.9;39.5;40.6;39.7;40.3;41.7];
SIL=[29.4;30.9;32.9;26.9;28;28.8;30.1;29.4;28.8;30];
Tonality=[0.133;0.128;0.113;0.153;0.14;0.131;0.118;0.133;0.203;0.18];
Kurtosis=[2.2;2.2;2.2;2.44;2.49;2.48;2.45;2.2;2.39;2.38];
subjective=[7.5;7.02;6.94;7.91;7.96;7.91;7.78;7.42;7.86;7.47];
metriche=[Loudness FlucStr Roughness Sharpness Leq SIL Tonality Kurtosis];
%% result of simple regression
for jj=1:size(metriche,2)
p=polyfitn(metriche(:,jj),subjective,1);
R(jj)=p.R2;
coeff(:,jj)=p.Coefficients;
end
But i need to calculate :polyfitn,coefficients and R2 with for loop (multiple regression), with every 2 possible combination of vector column metriche.
how can i do this?

Answers (1)

Yogesh Khurana
Yogesh Khurana on 17 Dec 2019
polyfitn function fits a general polynomial regression model in n dimensions. You have implemented the loop in getting the results for all metriche (your variable name). For getting the results for every 2 possible combination, you can try two ways :
First, just add two for loops to get each element correspond to every other element like:
for ele1 = 1: numel(metriche, 2)
for ele2 = ele1 : numel(metriche, 2)
data = [ele1; ele2];
% Your code
end
end
Second, you can use an inbuilt MATLAB function, combnk, to get enumeration of the combinations of your vector. You need to pass the vector and the number of elements you want to take at a time.
I am attaching some links for your reference on polyfitn and combnk. Please refer to the following links for more details:

Community Treasure Hunt

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

Start Hunting!