Calculate % difference for 2 vectors into a matrix

8 ビュー (過去 30 日間)
Coulter Johnston
Coulter Johnston 2022 年 3 月 3 日
編集済み: Alberto Cuadra Lara 2022 年 3 月 3 日
Hi,
I have two vectors of average firing rates of the same population of neurons when exposed to different stimuli. These vectors are both the same size (1x158). I want to create a matrix that would be 158x158 of the percentage difference between each of the firing rates, then make a heatmap of this matrix. Is there any function that could help to directly make this matrix? I have used the following code so far to try to calculate the percentage difference, but it doesnt seem to be working correctly as the values in each column of the matrix are all the same:
diffMat = zeros(length(FR),length(FR2));
for i = 1:length(FR)
for j = 1:length(FR2)
diffMat(i,j) = abs(FR2(j)-FR(i)./FR(i))*100;
end
end
  2 件のコメント
Matt J
Matt J 2022 年 3 月 3 日
but it doesnt seem to be working correctly as the values in each column of the matrix are all the same:
You should run that test for us (here, not on your local computer) so we can see what you mean. For now, all we can do is assume that FR2 has been made all zeros by mistake.
Alberto Cuadra Lara
Alberto Cuadra Lara 2022 年 3 月 3 日
編集済み: Alberto Cuadra Lara 2022 年 3 月 3 日
Hello Coulter,
I know you already have your answer. The error here was in your definition of diffMat. The parentheses are missing; otherwise FR(i)./FR(i) is 1, so you'll always get the same row values.
% Data
N = 3;
FR = randn(1, N);
FR2 = randn(1, N);
% Original
diffMat = zeros(length(FR),length(FR2));
for i = 1:length(FR)
for j = 1:length(FR2)
diffMat(i,j) = abs(FR2(j)-FR(i)./FR(i))*100;
end
end
diffMat
diffMat = 3×3
148.3783 137.3370 94.9191 148.3783 137.3370 94.9191 148.3783 137.3370 94.9191
% Correct
for i = 1:length(FR)
for j = 1:length(FR2)
diffMat(i,j) = abs((FR2(j)-FR(i))./FR(i))*100;
end
end
diffMat
diffMat = 3×3
28.3443 44.6982 107.5256 148.6678 137.5604 94.8887 79.1378 83.8991 102.1910
% Best approach
diffMat = abs(1 - FR2./FR')*100
diffMat = 3×3
28.3443 44.6982 107.5256 148.6678 137.5604 94.8887 79.1378 83.8991 102.1910

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

採用された回答

Matt J
Matt J 2022 年 3 月 3 日
編集済み: Matt J 2022 年 3 月 3 日
I don't see any errors in your version, but this is shorter and faster:
diffMat=abs( 1 - FR2./FR')*100;

その他の回答 (1 件)

David Hill
David Hill 2022 年 3 月 3 日
FR1=rand(1,158);FR2=rand(1,158);
[fr1,fr2]=meshgrid(FR1,FR2);
percentDifference=(fr2-fr1)./fr1*100;

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by