Append results into an array in a for loop as in python

12 ビュー (過去 30 日間)
Reuben Addison
Reuben Addison 2023 年 4 月 1 日
回答済み: Cris LaPierre 2023 年 4 月 1 日
I created an array to store results from a for loop, looks like it seems to break
vertices = zeros(length(Data.X3),1);
for i = 1:length(Data.X3)
[vertices(i), ~]= Vertex([Data.X3(i); Data.Y3(i); Data.Z3(i)])
end
But I am not getting the desured results
  6 件のコメント
dpb
dpb 2023 年 4 月 1 日
Besides @Cris LaPierre's Q?, what is the content of Data.X, ...? Is it actually the data itself or an index into the data? As written, the expression [Data.X3(i);Data.Y3(i);Data.Z3(i)] creates a column 3-vector and making the assumption that Vertex() is an array, then each call will return a 3-vector of the values at those indices, if they are indeed valid indices into the array. If they're actually values instead, then "Boom!"; either likely will be indices outside the range of the array or invalid floating point values attempted to be used an indices.
On the LHS, the "~" tells MATLAB to throw away any second returned value from a function call and the single index (i) on the preallocated array is attempting to store three elements into a single location, so that is bad syntax from multiple points of view.
IF the Data structure elements are indeed valid indices into the Vertex array, then the output vector size must be 3X the size of each of those if the idea is to concatenate all into one long column array; given that it is preallocated only to that size vertically and by the use of a second element in the LHS expression, I'm guessing the intent was to produce a 2D array of the X,Y,Z locations. If that is the case, and also guessing that the "3" on the stuctuure names implies a 3D array, then "the MATLAB way" using vectorized operations would simply be
vertices=[Vertex(Data.X3, Data.Y3,Data.Z];
The above makes lots of assumptions, but without the details of what really have, it's about best can make a stab at -- other than the syntax issues.
Reuben Addison
Reuben Addison 2023 年 4 月 1 日
So Vertex is a function that returns the vertex number and vertex coordinates (I am trying to get rid of the vertex number hence the tilde and RHS goes through a data table to pick out some numbers to compute the vertex number and coordinates whereas the LHS is suppose to store the results

サインインしてコメントする。

採用された回答

Cris LaPierre
Cris LaPierre 2023 年 4 月 1 日
Not sure what the desired results are, but here's a sample of your code. It works as I'd expect. Perhaps you can be more clear on what is not working for you.
% Make up some data
X3 = rand(10,1);
Y3 = rand(10,1);
Z3 = rand(10,1);
Data = table(X3,Y3,Z3)
Data = 10×3 table
X3 Y3 Z3 ________ _______ ________ 0.26829 0.41537 0.57013 0.54631 0.64146 0.97118 0.24915 0.58069 0.31743 0.83112 0.80822 0.42471 0.035294 0.52805 0.59974 0.46136 0.57216 0.47514 0.31929 0.89843 0.11462 0.22365 0.53646 0.36506 0.94193 0.6462 0.87688 0.11137 0.65684 0.056632
% your original code
vertices = zeros(length(Data.X3),1);
for i = 1:length(Data.X3)
[vertices(i), ~]= Vertex([Data.X3(i); Data.Y3(i); Data.Z3(i)]);
end
% View results
vertices
vertices = 10×1
3 3 2 1 3 2 2 2 1 2
% made up function for demonstration purposes
function [vertex_number, vertex_coord] = Vertex(D);
[vertex_coord, vertex_number] = max(D);
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by