For over three dimensional data
3 次查看(过去 30 天)
显示 更早的评论
I have a data of the form: A = 7x9x7, where u = [1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6]. Please how can loop over u. I tried
for j = 1:length(u)
B(j) = squeeze(A(j,1,:));
end
such that I have: B(1) = squeeze(A(1,1,:)), B(2) = squeeze(A(2,1,:)), B(3) = squeeze(A(3,1,:)) ... until B(7) = squeeze(A(7,1,:))
but i received an error: Index in position 1 is invalid. Array indices must be positive integers or logical values.
1 个评论
Rik
2023-2-1
I recovered the removed content from the Bing cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.
回答(1 个)
MarKf
2023-1-4
How is my whole Uni requesting help like this? Surely there are some people who could help within already...
Anyway, you should specify B(j,:) = squeeze(); maybe also preallocalocate before the loop B=nan(7,7) or nan(size(A,[1,3])) so you know what you are getting.
1 个评论
MarKf
2023-1-4
编辑:MarKf
2023-1-4
Actually now that I think about it, that's not the error you would be getting in that case (it'd be something about indices and left/right sides)
A = ones([7,9,7]); u = [1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6];
for j = 1:length(u)
B(j,:) = squeeze(A(j,1,:));
end
So maybe you did something else, like writing for j = u instead.
Besides, there is no need for a loop if you just need B = squeeze(A(:,1,:));
另请参阅
类别
在 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!