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 comentarios

millercommamatt
millercommamatt el 29 de Oct. de 2021
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 el 29 de Oct. de 2021
Editada: Stephen23 el 29 de Oct. de 2021
"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.
Pelajar UM
Pelajar UM el 29 de Oct. de 2021
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?
Stephen23
Stephen23 el 29 de Oct. de 2021
"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.
Pelajar UM
Pelajar UM el 30 de Oct. de 2021
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 el 30 de Oct. de 2021
Editada: Stephen23 el 30 de Oct. de 2021
"...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:
Pelajar UM
Pelajar UM el 30 de Oct. de 2021
Editada: Pelajar UM el 30 de Oct. de 2021
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));
Stephen23
Stephen23 el 30 de Oct. de 2021
Editada: Stephen23 el 31 de Oct. de 2021
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 el 30 de Oct. de 2021
Editada: Pelajar UM el 31 de Oct. de 2021
Perfect! Thank you so much.
Small error: your code mixes k and i.
Stephen23
Stephen23 el 31 de Oct. de 2021
"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{:})

Iniciar sesión para comentar.

 Respuesta aceptada

millercommamatt
millercommamatt el 29 de Oct. de 2021

0 votos

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 comentarios

Pelajar UM
Pelajar UM el 29 de Oct. de 2021
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
Pelajar UM
Pelajar UM el 29 de Oct. de 2021
Wonderful. Thanks a lot.
Stephen23
Stephen23 el 29 de Oct. de 2021
"Yes, this is exactly what I wanted"
and is a slow, complex, inefficient, obfuscated approach this task.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 29 de Oct. de 2021

Comentada:

el 31 de Oct. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by