Extract sub-matrix compute correlation coefficient 2D case iteratively

7 ビュー (過去 30 日間)
Joshua
Joshua 2020 年 2 月 18 日
コメント済み: darova 2020 年 2 月 18 日
Hello friends, I will like to extract 2x2 sub-matrices from a large matrix and calculate correlation coefficient for each of the 2x2. Loop the procedure over the main matrix and store the correlation coefficient in output file. the for loop gave some error, I will appreciate guide to fix the code. Thank you.
% infile
file1='d.dat'; %
file2='b.dat'; %
% Outfile
correlout='correl.dat'; %
numcols=20; %
numrows=20;
%open file
fid1=fopen(file1);
f1=fscanf(fid1,'%f',[numcols numrows]);
fclose(fid1);
fid2=fopen(file2);
f2=fscanf(fid2,'%f',[numcols numrows]);
fclose(fid2);
%reshape
Refid1=reshape(fid1,[numcols,numrows]);
Refid2=reshape(fid2,[numcols,numrows]);
%pre-allocate
ccoef=zeros(numrows,numcols);
cRes=zeros(numrows,numcols);
subf1(i)=zeros();
subf2(i)=zeros();
%loop over
for i=1:numcols
for j=1:numrows
subf1(i) = Refid1(i:i+1, j:j+1);
subf2(i) = Refid2(i:i+1, j:j+1);
ccoef(i)=corrcoef(subf1(i),subf2(i));
cRes(i)=ccoef(1,2);
end
end
%outfile
correlout=cRes(i);
%write to file
fid=fopen(correlout, 'w');
fprintf(fid,'%6.2f\n',correlout);
fclose(fid);

採用された回答

darova
darova 2020 年 2 月 18 日
編集済み: darova 2020 年 2 月 18 日
  • Sometimes i+1 > numcols. What will happen then? (RED)
  • Left side has one number, right one - 4. (GREEN)
  6 件のコメント
Joshua
Joshua 2020 年 2 月 18 日
Many thanks. The loop works but cannot write to output file. Any suggestion on this please.
darova
darova 2020 年 2 月 18 日

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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