Basic question about loops

Very basic question: why is this not working?
instead of
Idx2=Idx(:,2)
Idx3=Idx(:,3)
Idx4=Idx(:,4)
Idx5=Idx(:,5)
I wrote
for i=2:5
Idx(i)=Idx(:,i)
end
The error is: Unable to perform assignment because the left and right sides have a different number of elements.

10 个评论

Let me make sure I understand what you are trying to achieve.
You want to break out each column - or some subset of columns - of a 2-D array into their own variables?
Stephen23
Stephen23 2021-10-29
编辑:Stephen23 2021-10-29
"Very basic question: why is this not working?"
Because you chose a complex and indirect way to write your code.
Numbering variables like that is a sign that you are doing something wrong.
Forcing meta-data (e.g. pseudo-indices) into variable names is one way that beginners force themselves into writing slow, complex, inefficient, obfuscated, buggy code that is difficult to debug.
Your task could be solved simply and much more efficiently using indexing.
yes, i have seen this thread before but can't quite understand how indexing works.
Can you take a line of my code and re-write it?
"but can't quite understand how indexing works. "
You are using indexing already on the RHS. That is how indexing works.
Splitting the data up into numbered variables is most likely a superfluous step: why can't you just access the data using that indexing? Or if splitting is required, just use a cell array (e.g. via NUM2CELL) rather than lots of variables.
Ok. I think I'm starting to understand what you mean, but how do I output the results to a single array?
Like here centroid is a 10x3 array, so for i=2:10, I expect to get 10x(3x8) array but I still get a 10x3 array.
for i=2:10
[sortedcentroid]=centroid(Idx(:,i),:);
end
Stephen23
Stephen23 2021-10-30
编辑:Stephen23 2021-10-30
"...how do I output the results to a single array?"
Use indexing on the LHS, just like you are already doing on the RHS.
Preallocate the ND array using nan(..) and then index into it. If the number of elements returned on each iteration are different then you will have to use a cell array instead:
I have tried but it keeps saying the number of elements are different on reach side. How should I index the LHS?
Idx is 11446x10 and centroid is 11446x3.
I have added the preallocation:
sortedcentroid = nan(size(centroid));
The size of each RHS depends on the indices, which you have told us nothing about. So I can only presume that they are different on each iteration. In that case, you could use a cell array:
C = cell(1,10);
for k = 2:10
C{k} = centroid(Idx(:,k),:);
end
Pelajar UM
Pelajar UM 2021-10-30
编辑:Pelajar UM 2021-10-31
Perfect! Thank you so much.
Small error: your code mixes k and i.
"Small error: your code mixes k and i"
Fixed, thank you!
If the content of C are of suitable sizes then after the loop you could concatenate them back into one array, e.g.:
A = cat(3,C{:})

请先登录,再进行评论。

 采纳的回答

You could make use of genvarname, but this would work too.
for i = 2:5
eval(['Idx' num2str(i) ' = Idx(:,' num2str(i) ');']);
end
When you start using functions like genvarname and eval, it's usually a sign that you should be approaching your task in a different way.

4 个评论

Yes, this is exactly what I wanted. Thanks. But what happens if I'm transposing a variable. Something like this:
angle2 = transpose (rad2deg (atan2(vecnorm(cross(N',sortedN2')), dot(N',sortedN2'))))
angle3 = transpose (rad2deg (atan2(vecnorm(cross(N',sortedN3')), dot(N',sortedN3'))))
angle4 = transpose (rad2deg (atan2(vecnorm(cross(N',sortedN4')), dot(N',sortedN4'))))
angle5 = transpose (rad2deg (atan2(vecnorm(cross(N',sortedN5')), dot(N',sortedN5'))))
Something like this. Study how to manipulate strings.
for i = 2:5
eval(['angle' num2str(i) ' = transpose (rad2deg (atan2(vecnorm(cross(N'',sortedN' num2str(i) ''')), dot(N'',sortedN' num2str(i) '''))));']);
end
Wonderful. Thanks a lot.
"Yes, this is exactly what I wanted"
and is a slow, complex, inefficient, obfuscated approach this task.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by