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 Comments

You want to spot zero values with a specified tolerance?
Yes.I actually need to get zero values of a matrix including negative and positive values for different ranges of Ma and K
"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?

Sign in to comment.

 Accepted Answer

Use interp2 and fzero, something like this:
>> 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 Comments

I attached a Ma-k.mat file. Thanks for your help.
Thanks for your response. What is the knew?? you should obtain 3 values for Ma from your matrix !! Why you got one?
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
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
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?
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.
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
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.
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.

Sign in to comment.

More Answers (0)

Tags

Asked:

on 20 Feb 2018

Edited:

on 20 Feb 2018

Community Treasure Hunt

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

Start Hunting!