get the output of an if clause in the order

Dear All,
My code is so long and unfortunately cannot copy all of it, but I guess I know what is the problem with the code. I do appreciate anyone's help to solve the problem. Thanks in advance.
I have a zero matrix defined as :
H = zeros (1, 100);
% Basically I defined this zero matrix to export the output into it. If you have any other alternatives which satisfy the needs, I am so welcome.
Here is my if clause which investigates for a desired condition.
if nnz(Rhh) ~= 0
% where Rhh is a matrix of (1,80) and defined in every step k, in other words for each k, there is only one Rhh matrix assigned to that specific k. For most of the k values, Rhh is a zero matrix.
What I simply need to do is write the first 100 k's which have the non-zero Rhh. The k's has to be written in the order into the matrix H. Here is the code I have written for it and I know what is the wrong with the code. However, cannot fix it.
for j=1:100
if (H(1,j)~=0)
continue
else
H(1,j)=k
end
Obviously the code does not work out. As soon as the first k is found, all of the elements of the matrix H will turns to the first value of k. Thus, there will be not zero elements left for other 99 k values.
I hope you kindly help me to figure it out.
All the Best,
HRJ

Réponses (1)

Star Strider
Star Strider le 13 Août 2015
I’m not certain that I follow everything in your Question. I also have no idea what ‘k’ is. However, assigning ‘H’ would seem to me to be:
for j=1:100
if (H(1,j)~=0)
continue
else
H(1,j)=k(j);
end
end
There are more efficient ways to do that (using logical indexing), but considering I don’t know everything I would like to about what you’re doing, I would just change the loop to assign the appropriate element of ‘k’ to ‘H’.

4 commentaires

Well, thank you so much but the answer does not work out. Let me give you some explanations.Believe me the code is so gigantic :-) this is what i simply want:
H = zeros (1, 100);
for k= 0:10000
Obtain Matrix Rhh via some calculations
if nnz(Rhh) ~= 0
for j=1:100
if (H(1,j)~=0)
continue
else
H(1,j)=k;
end
end
end
In other words, I want to set the first element of matrix H i.e. H(1,1) equal to the first k which has a non-zero Rhh matrix, the second element of matrix H i.e. H(1,2) equal to the second k which has a non-zero Rhh matrix and etc. Is this explanation helpful? The code I wrote is not addressing my needs doe to the reason explained in the main body of my question.
I can’t test this on your code, so bear that in mind if you decide to try it. I’m keeping your loop because although it is obvious from the code you posted that ‘H’ is uniformly zero so the first if condition will never be met, I am assuming you are doing something to ‘H’ before the loop to define it differently.
k = 0:10000; % Create Data
Rhh = randi([0 5], 1, 150); % Create Data
Rhh_idx = Rhh~=0; % Logical Vector: Non-Zero ‘Rhh’
kv = k(Rhh_idx); % Values of ‘k’ Corresponding to ‘Rhh_idx’
for j=1:100
if (H(j) == 0)
H(j)=kv(j);
end
end
I streamlined the loop, because you’re not doing anything if ‘H(j)’ is not zero.
I’m still not sure what you want.
Dear Star,
I do appreciate your patience on this question, however, your answer , with respect, does not address my question.
I have a for loop with k as the identifier. for each value of k, a unique matrix of Rhh is obtained. if nnz(Rhh)==0, we do not need to do anything. But if nnz(Rhh)~=0, i need to have the k as the output. Now imagine only 5 values of k among 1:100000 have a non-zero Rhh matrix. Let's say K= 100, 5000, 6000, 7666 and 99999. I need the matrix H to be in the following format and contains of all of these k values i.e.
H = [100, 5000, 6000, 7666, 99999]
Star Strider
Star Strider le 13 Août 2015
I’m lost. I’m obviously not understanding what you want and am not providing you an answer you want.
I’ll delete my Answer in a few minutes.

Cette question est clôturée.

Question posée :

le 13 Août 2015

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by