Need a better way to loop through a Matrix column and then output the row
16 ビュー (過去 30 日間)
古いコメントを表示
I have a matrix (Nodal_Coordinates). I'm trying to loop through column 1 to find Node 1, Node 2... Node n. Then once Node_1 is found, I need it to display the respective row and only column 2 and column 3. The matrix row size can change with respect to the input, so it needs to be flexible on evaluating the matrix up to nth nodes. However, the columns will always be three. Col 1 is the node number, Col 2 is the x coordinate, Col 3 is the y coordinate. Below is the code I've been trying, but it outputs the entire row instead of just the 2nd and 3rd values of the row, and it's not flexible for nth rows... it only works for a fixed number of rows. Thanks for the help.
Nodal_Coordinates =
1 0 0
2 3 0
3 6 0
4 9 0
8 0 3
7 3 3
6 6 3
5 9 3
for i = Nodal_Coordinates(:,1)
for i = 1
Node_1 = Nodal_Coordinates(i,:)
for i = 2
Node_2 = Nodal_Coordinates(i,:)
for i = 3
Node_3 = Nodal_Coordinates(i,:)
for i = 4
Node_4 = Nodal_Coordinates(i,:)
for i = 5
Node_5 = Nodal_Coordinates(i,:)
for i = 6
Node_6 = Nodal_Coordinates(i,:)
for i = 7
Node_7 = Nodal_Coordinates(i,:)
for i = 8
Node_8 = Nodal_Coordinates(i,:)
end
end
end
end
end
end
end
end
end
1 件のコメント
Turlough Hughes
2020 年 3 月 2 日
Your code tells me you are quite new to matlab. I would recommend you do the introductory onramp course for matlab.
採用された回答
Tom Holz
2020 年 3 月 2 日
編集済み: Tom Holz
2020 年 3 月 2 日
Something like this
for k = 1:size(Nodal_Coordinates,1)
% Find the index of the kth row
ind = find(Nodal_Coordinates(:,1) == k, 'first');
% Extract the rest of that row, skipping the first column
Node_Data = Nodal_Coordiantes(ind,2:end);
% Do what you want with that data
disp(Node_Data);
end
However, you might be able to simply this process by sorting the data first:
% Sortrows defaults to sorting by the first column in numeric order,
% and if no two rows have the same index in the first column,
% this will produce the desired result.
Nodal_Coordinates = sortrows(Nodal_Coordinates);
% Now you can just loop through the matrix and grab the data without using find().
for k = 1:size(Nodal_Coordinates,1)
ind = Nodal_Coordinates(k,1);
Node_Data = Nodal_Coordinates(k, 2:end);
end
5 件のコメント
Stephen23
2020 年 3 月 2 日
"Node_1 ... Node_2 ... Node_3 ... So basically have those outputs become variables?"
Putting numbers into variable names indicates that you are doing something wrong.
Accessing variable names dynamically is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know why:
The simple and efficient solution is to use one array and access the data using indexing.
Tom Holz
2020 年 3 月 2 日
I agree with Stephen. If you want to display the data, look into a function like printf:
for k = 1:size(Nodal_Coordinates,1)
ind = Nodal_Coordinates(k,1);
Node_Data = Nodal_Coordinates(k, 2:end);
fprintf('Node #%d: %s\n', ind, num2str(Node_Data));
end
If you want to reuse the data later, consider putting it into another variable, like a cell array. Cell arrays are quite versatile and are worth learning about.
nodeCount = size(Nodal_Coordinates,1);
nodes = cell(nodeCount,1);
for k = 1:nodeCount
ind = Nodal_Coordinates(k,1);
Node_Data = Nodal_Coordinates(k, 2:end);
nodes{k} = Node_Data;
end
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!