data from one array to another
显示 更早的评论
I am trying to put the coresponding value from a set of data from the max value from each column into a select array based on the row the max value occured in and i'm not sure what i've done wrong.
particle.clustervalues is a 15x100 array, x is 100x2 array (data) and particle(i).cluster is 15 arrays where if the max value in column 1 clustervalues occurs on row 9, row x(1) goes into array 9. Any help would be appreciated.
2 个评论
kamran waheed
2021-3-27
the cyclist
2021-3-27
I am finding it impossible to help. Here are the reasons why:
- Your code is completely uncommented. It is not possible for me to follow the logic of what each line is supposed to be doing.
- I don't really understand the problem. When you say, "put the coresponding value from a set of data from the max value from each column", it is very unclear to me what you mean. What data? What column?
- The way you have structured your data is quite complex. Maybe this is necessary, but it is still difficult to understand.
- The problem is large enough that it is not easy to focus on a small piece, to see what is wrong.
I would recommend the following, to get help:
- Comment everything in the code, so that another user can understand what is going on. (This will also help you 6 months from now, when you have forgotten everything going on. Trust me.)
- Every place that you currently have "100", change that to a parameter, with a descriptive name. (Maybe it is something like NPARTICLES = 100;)
- Do the same thing for all other constant parameters
- For code testing purposes, make all of those parameters the smallest value the makes sense. For example, instead of 100, maybe it can be 3.
- Because you are using random number, use rng default at the beginning of your code (for testing only), so that you get the same random numbers each time.
- Post your problem again (not just as a comment here), so that more people see it again. Feel free to tag me with @the cyclist, and I'll try to take a look again.
Doing all of this may also help you discover the error on your own, without our help. It is all simply good coding practice.
I hope that helps.
回答(1 个)
the cyclist
2021-3-27
I can't tell what you intend to get from this expression
[x(j),:]
which is not valid MATLAB syntax. x(j) is the j-th element of x, and the square brackets would be used to concatenate two expressions. But concatenating to the colon doesn't make sense.
The colon can mean "all elements along this dimension" ... but you are not indexing into a variable.
So, I'm confused about what you are trying to do.
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!