I have a matrix named as v_s and want to find first 1 value's location with using any reference matrix. v_s has 3 columns and locations should create a matrix named as v_d.
I have already coded this operation, but don't know where is my mistake. May you help me?
Thanks,
Note: first two statement(==0 and ==99) can be stay fixed and not be changed.
b=3;
v_d=rand(b,1);
v_s=[0 0 0 0 0 1; 0 0 0 0 0 1; 0 0 0 0 1 0]
for j=1:b;
if v_s(j,:)==0
v_d(j)=0
elseif v_s(j,:)==99
v_d(j)=99;
elseif v_s(j,:)~=0
ref=[6 5 4 3 2 1];
v_d(j)=ref(find(v_s(j,:)==1,1,'first'))
end
end

5 commentaires

madhan ravi
madhan ravi le 8 Avr 2019
Please explicitly show how your expected result should look like.
Alex Yande
Alex Yande le 8 Avr 2019
v_d=[1 1 2]
madhan ravi
madhan ravi le 8 Avr 2019
So according to what I understand you want to find first nonzero entry in each row in reverse order?
Alex Yande
Alex Yande le 8 Avr 2019
Modifié(e) : Alex Yande le 8 Avr 2019
This code is simplified version of my work. Normally, i am analyzing big data. So, there is two unnecessary statement here.
Alex Yande
Alex Yande le 8 Avr 2019
Partially yes. I want to find location of first one by comparing ref matrix.

Connectez-vous pour commenter.

 Réponse acceptée

AstroGuy1984
AstroGuy1984 le 8 Avr 2019
What's going wrong is in your logicals. For example, the first logical on the first loop will do
if [0 0 0 0 0 99] == 0
Which will return [1 1 1 1 1 0].
The way MATLAB interprets this is FALSE. Because if you have one thing false, it has failed. I suspect you're really wanting to say if the whole row of the matrix is zeros mark v_d(j) as 0. While in that case, it would work programmatically, it is probably clearer to use the all() command anyway. If nothing else, because it is clear what you're meaning to another programmer.
if all(v_s(j,:) == 0)
Next, we would get to this
elseif v_s(j,:)==99
or for the first loop,
elseif [0 0 0 0 0 99] == 99
Here, the MATLAB will evaluate this as [0 0 0 0 0 1], or FALSE. What you're really trying to ask here is if there's a 99 in the the row. any() will be a quick solve here. Such as:
elseif any(v_s(j,:) == 99)
Which is asking if ANY of the values in that row are 99.
I am unsure why you're reversing the number on the final else loop, but again your logical is going to turn an array not a single value. If you follow the other conventions, you shouldn't need another logical and can just go with else, and then proceed to find where the 1 is in that row.

4 commentaires

Alex Yande
Alex Yande le 8 Avr 2019
Your solution works correctly in MATLAB, but when i use your approach in SIMULINK:
Error:Subscripted assignment dimension mismatch: [1] ~= [0].
Error in 'FILE_NAME Function6' (line 11)
v_d(j)=ref(find(v_s(j,:)==1,1,'first'))
Error:An error occurred while running the simulation and the simulation was terminated
Caused by:
Simulation stopped because of a runtime error.
Guillaume
Guillaume le 8 Avr 2019
What that error is telling is that you're trying to assign zeros elements to an array with one element. This has nothing to do with Simulink and will always be an error.
Now the most likely reason that you're trying to assign zeros element would be because find finds nothing. Indeed, if there's not a single 1 in the row, there's nothing to find.
If you did expect to always have at lease one 1 in each row, then you've made a mistake earlier in your code.
The whole code you've written looks very awkward. Perhaps if you explained what it is supposed to do, we could give you something much better. It's likely that no loop or if is needed.
Alex Yande
Alex Yande le 9 Avr 2019
Modifié(e) : Alex Yande le 9 Avr 2019
Thank you Guillaume,
As i said before, i use this code for another operations of my project. So, i agree to strange view of this written code. I understand your explanation and now i am sure, desired work can not be succesfull with this type of code. I will create another theory for my project.
Alex Yande
Alex Yande le 9 Avr 2019
Thanks for all of the answers. :))

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by