How can I extract zero values from a big Matrix
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hello, I have a big matrix (see attachment) that is a result of calculation for different K (row) and different Ma (column) ( I wrote a matlab code for getting this matrix). How can I extract the Ma corresponding with zero values of this matrix for different K (what combinations of K and Ma give me zero?) using fzero function? Thank you very much for your help in advance.
3 件のコメント
Birdman
2018 年 2 月 20 日
You want to spot zero values with a specified tolerance?
Ramin Rabani
2018 年 2 月 20 日
Yes.I actually need to get zero values of a matrix including negative and positive values for different ranges of Ma and K
Stephen23
2018 年 2 月 20 日
"what combinations of K and Ma give me zero?"
What does this mean? Given the data that you have shown none of the combinations of K and Ma have zero value in that table. If you want to interpolate the data between those K and Ma values then there will be infinite combinations which have a zero value, so which of those infinite locations are of interest to you?
採用された回答
>> V = [-2.5,-2.5,-2.5;-2,-1,-0.5;0,1,2;10,11,12]
V =
-2.50000 -2.50000 -2.50000
-2.00000 -1.00000 -0.50000
0.00000 1.00000 2.00000
10.00000 11.00000 12.00000
>> K = [1,2,3];
>> M = [10,90,113,159];
>> Knew = 2;
>> fun = @(m)interp2(K,M,V,Knew,m);
>> Mnew = fzero(fun,mean(M))
Mnew = 101.5
16 件のコメント
Ramin Rabani
2018 年 2 月 20 日
I attached a Ma-k.mat file. Thanks for your help.
Ramin Rabani
2018 年 2 月 20 日
Thanks for your response. What is the knew?? you should obtain 3 values for Ma from your matrix !! Why you got one?
Ramin Rabani
2018 年 2 月 20 日
in addition, one of the values of the matrix is exactly zero, why you didn't get its Ma?
"What is the knew??"
It is the value of K that fzero is currently using to find the zero crossing (interpolated from your data), in order to find the value Mnew.
"you should obtain 3 values for Ma from your matrix !! Why you got one?"
Because that is not how fzero works. fzero will only find one zero crossing for one Knew, not multiple different zero crossings for different Knew. If you want to get different Ma values for different Knew values then you will need to put my answer into a loop, and run it for each Knew. Remember to preallocate the output array.
"one of the values of the matrix is exactly zero, why you didn't get its Ma?"
Because that is not how fzero works. If you want to locate any zeros in an array then fzero is not the correct tool: just use logical indexing. If you want to interpolate data to find positions that are not in the original data but are estimated to have the values zero then use fzero. I have no idea what you want to do, only you know that.
Your statement is also incorrect, it is easy to show that no value in that data matrix "is exactly zero":
>> any(gridgrowth(:)==0)
ans = 0
Ramin Rabani
2018 年 2 月 20 日
You didn't understand my question well. As I explained above, I have a matrix for different k and Ma. The values inside the matrix are positive and negative (there are not any zero values in the matrix). I need to obtain Ma for different K corresponding to zero values of this matrix.
" I need to obtain Ma for different K corresponding to zero values of this matrix."
There are no zero values in your matrix:
>> any(gridgrowth(:)==0)
ans = 0
The smallest magnitude value in the entire matrix is
>> min(abs(gridgrowth(:)))
ans = 0.0072379
which disagrees with your statement " one of the values of the matrix is exactly zero".
Please explain what you really want, without referring to "zero values of this matrix" (because they simply do not exist). If "one of the values of the matrix is exactly zero" as you write, then please tell me its index. I am curious to know where it is.
What I showed you in my answer is how to interpolate that data and find any one zero crossing corresponding to any K value. You can easily put this into a loop to find as many Ma values whatever K values you desire, e.g. for the same K input values:
V = [-2.5,-2.5,-2.5;-2,-1,-0.5;0,1,2;10,11,12];
K = [1,2,3];
M = [10,90,113,159];
Mout = nan(size(K));
for ii = 1:numel(K)
fun = @(m)interp2(K,M,V,K(ii),m);
Mout(ii) = fzero(fun,mean(M))
end
giving:
Mout =
113.000 101.500 94.600
Ramin Rabani
2018 年 2 月 20 日
Yes, There are no zero values in the matrix. I want to obtain these zero values and after that I need to extract the Ma for these zero values( I need Ma corresponding to zero values inside the matrix( we should obtain from fzero function)). I don't know how I can explain my question!
"I want to obtain these zero values"
What zero values? The only possible way to get zero values would be to interpolate your data (which is what my answer did).
I will assume that that is what you want.
"...and after that I need to extract the Ma for these zero values..."
There are infinite possible locations of zero-crossings, if you consider all real numbers of K and Ma.
I will assume that you want only a finite number of those zero-crossings, corresponding to a finite list of K values. In my answer I explained how to do that. In my last comment I showed you how to do that, for the same K values as the data matrix has.
Are my assumptions incorrect?
Ramin Rabani
2018 年 2 月 20 日
編集済み: Stephen23
2018 年 2 月 20 日
Many thanks.Now it works for my problem. could you tell me what dose it mean mean(M)? Moreover, the code I have is;
nk=5;
arrk=linspace(2,10,nk);
nMa=5;
arrMa=linspace(0,200,nMa);
Mout = nan(size(K));
for ii = 1:numel(arrk)
fun = @(m)interp2(arrk,arrMa,eige1,arrk(ii),m);
Mout(ii) = fzero(fun,mean(arrMa))
end
I want to know how I can also get the k?
"could you tell me what dose it mean mean(M)?"
I used the mean of the Ma values as the starting estimate for fzero: read the fzero help for more information on this.
"I want to know how I can also get the k?"
If you mean the K values corresponding to the values in Mout then you have them already in arrk. As I wrote earlier, you can use any finite list of K values, this list is entirely up to you.
If you mean the K values corresponding to some finite list of Ma values (not those calculated above) then loop over that list instead, and make the appropriate changes to the code.
Ramin Rabani
2018 年 2 月 20 日
I mean how i can print K with Ma?
"I mean how i can print K with Ma?"
You never asked anything about printing before. And now you did not give any clues about where you want to print this data, or with what format. I will assume that you want to print the new data to the command window, in which case this will do it:
disp([arrk(:),Mout(:)])
E.g. for the loop that I used in my comment above:
>> disp([K(:),Mout(:)])
1.0000 113.0000
2.0000 101.5000
3.0000 94.6000
Ramin Rabani
2018 年 2 月 20 日
編集済み: Stephen23
2018 年 2 月 20 日
Thanks a lot for all your help. I am runing the code for:
nk=20;
arrk=linspace(2,10,nk);
nMa=20;
arrMa=linspace(0,200,nMa);
and it takes much time to get the values. Do you know how i can increase the speed of calculations
"Do you know how i can increase the speed of calculations"
As long as the data values are strictly monotonic increasing with both K and Ma then you could use a scattered interpolant or griddata, and find the values directly.
Ramin Rabani
2018 年 2 月 20 日
編集済み: Ramin Rabani
2018 年 2 月 20 日
Thanks. Where I should use these functions in the code? could you give me one example of your matrix that you wrote above.
@Ramin Rabani: unfortunately your data are not monotonically increasing with K. So perhaps the fastest solution would be a simple loop with interp1, like this:
S = load('Ma-K.mat');
gridgrowth = S.gridgrowth;
% Interpolate:
S = size(gridgrowth);
inpK = 1:S(2);
inpMa = linspace(0,200,S(1));
outMa = nan(size(inpK));
for k = 1:S(2)
tmp = gridgrowth(:,k);
outMa(k) = interp1(tmp,inpMa,0);
end
% Plot:
surf(inpK,inpMa,gridgrowth)
xlabel('K')
ylabel('Ma')
zlabel('V')
hold on
plot3(inpK,outMa,zeros(1,S(2)),'-*r')

Note that this code does not use fzero, which your original question specifically requested to use.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
