convert the field of a struct in a matrix
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a struct [71x71] with different fields including WA (attached);
I want to create a matrix A [71x71] with the values including in WA.
I have used this code:
for j=1:71
for i=1:71
a(i,j)=MaxST(i,j).WA
end
end
but gives an error 'Subscripted assignment dimension mismatch.'
Can you help me to solve the problem, thanks
0 个评论
采纳的回答
Stephen23
2017-4-26
编辑:Stephen23
2017-4-26
Because some of the WA fields are empty, you will have to use a cell array:
out = reshape({MaxST.WA},[71,71])
If all of the fields were scalar numerics, you would be able to put them into a simple numeric array:
out = reshape([MaxST.WA],[71,71])
Both of these use the syntax shown in the documentation:
0 个评论
更多回答(1 个)
KSSV
2017-4-26
Note that few values of the structure are empty..that's why error popped out. you may consider this:
load matlab2 ;
a = zeros(71,71) ;
for j=1:71
for i=1:71
if isempty(MaxST(i,j).WA)
a(i,j) = NaN ;
else
a(i,j)=MaxST(i,j).WA ;
end
end
end
4 个评论
Stephen23
2017-4-26
编辑:Stephen23
2017-4-26
"...for beginners the way to learn is by looping."
Why? Who said this? According to what pedagogical theory?
"Let them learn first and then they can go for vectorizing and use fancy inbuilt functions."
Why? Code vectorization is simpler than using loops! Simpler to write, simpler to read, simpler to understand... And as the documentation states "MATLAB® is optimized for operations involving matrices and vectors." When you state the beginners should not use vectorized code you are saying that you want beginners to have sub-optimal code.
I disagree.
I see absolutely no reason why beginners should not learn how to use MATLAB efficiently. That is what I would want, if I were learning MATLAB from scratch. Because basically you are saying that beginners are unable to understand MATLAB... and that later, after teaching them inefficient ways of solving problems, you will let them discover that everything they have been taught is a waste of time, and that you have been keeping the better methods to yourself. Why keep the better methods secret?
Would you want to be taught how to do things badly, and then later have to re-learn everything again?
What are "fancy inbuilt functions": do you mean the standard functions and syntax that MATLAB has, that anyone can use, and that are fully documented in the help? Why waste a nice high-level language like MATLAB as it was some low-level language like C++, and use ugly loops to solve everything?
KSSV
2017-4-26
编辑:KSSV
2017-4-26
All the users are not as fast as you think....I think when they code for the first time with out any coding experience, I don't think they can avoid loops. I am not asking them or some one to use bad ways or not to vectorize the code, to understand what happens for the first time, loop cannot be avoided.
另请参阅
类别
在 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!