Can I assign one element of a vector to a variable?

Hi everybody,
I have a question concerning my recent work. Isn't it possible to assign one element of a vector to a variable and get that saved in a cell array in a loop?
Later on I want to find the biggest element out of these and work with it. This is the error:
I also uploaded my code, as you can see the failure occures in line 219.
Here is a quick screenshot of the part of the code in case you don't need the full code.
Thank you very much for your help in advance, I'm a beginner at Matlab so thanks a lot everybody.
Johnny

 Accepted Answer

verschyl{allepunkte} is a row vector, whereas you are accessing it as a column vector. Reverse the order to correct the error. See the attached file. It should run without error.

10 Comments

Thanks a lot Ameer, woulnd't have found that one..
Have a nice day sir,
Johnny
I am glad to be of help.
I have another simple question, would be nice if you could help me out. Can I find a max in a cell array? Like in my code now the max value in verschy1{allepunkte}? I get this error code if I try to do so:
Thanks a lot again in advance and I am sorry for wasting your time.
Johnny
Is verschyl{allepunkte} a vector or scalar at this line of code? If it is a vector, then how many elements are in it? What is the value of allepunkte?
Wow big thank you for your fast replying. I´m doing the step where I first needed your help to make it a scalar i guess so I extracted one element of the vector. Then afterwards i have it written in a cell array every iteration.
So it is a scalar. In the cell array there are some free cells, does that matter?
Here is a screenshot of what verschy1{allepunkte} looks like here:
And now I want the biggest element out here, that would be great. Thank you once again Ameer.
Jonathan, you can try the following syntax to find the max of all the elements of the cell arrays
max([verschy1{:}])
I feel like what you are trying to do can be done quite easily in MATLAB with several if blocks. However, I don't understand the logic of your code. If the performance is fine for you and you understand the code, then you can continue using other; otherwise, you can attach a simple example of what you are trying to do in the first place. I will try to suggest a simple solution.
Hi Ameer,
I did it with the if-blocks earlier.
However I tried your suggestion and it works for me as well.
As your code seems shorter and more simple I think I am going to use it. That´s pretty nice thanks a lot.
I really don´t know whether my code is clever or if there is a simpler way to do it. I started MATLAB because of my bachelors thesis and this is one of the parts I have to do within my project. Can I describe what I am working on? Are you willing to give it a thought?
I really don´t know whether this is common but is there a way of contacting you?
It is not ultimately necessary, given the fact that I at least have a solution now, even if it maybe isn´t the fastest and shortest. If you offer your help anyways I´ll gladly help it.
Big THANK YOU Ameer for your help this far, you really helped me getting forward with my work.
Have a nice evening!! (If it is evening in your place, I´m from germany :))
Jonathan
Thanks!! I guess if it for a thesis, then execution speed might not be a critical requirement. It just needs to work to get the results. However, as a future reference, I recommend studying about vectorization in MATLAB and understanding the indexing in MATLAB. These are really powerful features that can help in writing code in just a few lines and avoid several if-else and for-loops. I wish you the best for your research.
It´s not you are right, even though I´m doing it for a company where we are building a simulation together with Catia and the CFD. However one iteration takes like 18 seconds, which are peanuts compared to what the CFD is gonna take so I guess we are just fine. Thanks a lot for your advice Ameer, I will do some research there.
Once again thanks for your overall help, wouldn`t have made it without your advice today.
I am glad to be of help!

Sign in to comment.

More Answers (0)

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!