Create a Vector Field from a Cell Array
3 次查看(过去 30 天)
显示 更早的评论
I am attempting to create a vector field from a cell array of stored vectors. However I am having some difficulty getting the vectors out of the cell array and graphed. I was wondering if anyone could tell me if there was a way to get the stored vectors from the cell array and plotted as a vector field besides typing each individual element in separately. Thank you for any help.
clear all;
clc
A = [-3, -2; 5, -1];
for i = 1:12
X_t{i} = [A*[i-1;i]];
X_t{i+1} = [A*[i;i-1]];
X_t{i+2} = [A*[i;i]];
end
figure
hold on
quiver(X_t{1}, X_t{2})
hold off
1 个评论
Jan
2013-4-5
Beside other unwanted stuff, "clear all" clears all breakpoints in the code. It is such a bad idea to prevent debugging conceptionally, that I can and must suggest to avoid this brute clearing.
Wat is a "vector field" here exactly? What does "I'm having some difficulties" exactly mean?
回答(1 个)
Jan
2013-4-5
编辑:Jan
2013-4-5
As long as you do not explain, what the code should achieve, I cannot suggest an improvement.
for i = 1:12
X_t{i} = [A*[i-1;i]];
X_t{i+1} = [A*[i;i-1]];
X_t{i+2} = [A*[i;i]];
end
You can omit the outer square brackets, as MLint most likely suggests already. Then in the first iteration you create the cell elements X{1}, X{2} and X{3}. In the 2nd iteration for i=2, you overwrite X{2} and X{3}, while X{4} is created. Therefore most elements of the cell vector X are overwritten several times and this is not useful. I cannot guess what you want to achieve instead.
Without the brute clearing header, you had a chance to use the debugger by setting a breakpoint. Then steppoing through the code would easily reveal, what's going on inside.
The documentation of quiver (type "doc quiver" in the command window) does not mention any cell arrays. The examples shown there do not use cell arrays also. So why do you want to choose this type to represent your data? Some matrices are more efficient and easier.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!