Thanks KSSV for your reply. Anyway, the problem is that I need in any case to obtain as output different variables whose name contains the subscript/index ( say _FIRE and _42, in the example above). So eventually I must separate them.
Loop with strings array
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi there,
I have two vectors, say: a_FIRE = [1 2 3] and a_42 = [4 5 6]. I would like to create other two vectors: s_FIRE = a_FIRE'*a_Fire and a_42'*a_42.
Since in reality I have 30 vectors whose index (in this example FIRE and 42) is different for each one, I should replicate 30 times the operation (writing 30 times s_{index} = a_{index}'*a_{index}). I would like to avoid doing this, since it is too time-consuming.
Is there any possibility to speed-up the process, maybe using a loop?
Please, I would be super pleased if the answer would contain the code, using the trivial example reported above. Because I haven't understood past answers to a similar question (structures, indexing...)
Thanks in advance
Edoardo
댓글 수: 3
Stephen23
2017년 9월 6일
편집: Stephen23
2017년 9월 6일
"Is there any possibility to speed-up the process, maybe using a loop?"
yes, quite easily if you had designed your code better.
"Since in reality I have 30 vectors whose index (in this example FIRE and 42) is different for each one"
And that is the problem right there. Sometimes beginners want to create or access lots of variables dynamically, and are then surprised at complex, slow, and buggy their code is. The solution is simple: do not try to access lots of variables dynamically, but use indexing instead. Indexing is simple, fast, and very efficient.
Read this to know more:
You should put your data into one array (numeric matrix, ND numeric, cell, struct, etc) and then use indexing in a loop. Trivially easy.
"I would like to avoid doing this, since it is too time-consuming."
And therefore you want to replace this with some buggy, slow code?
Learn to use indexing.
답변 (2개)
KSSV
2017년 9월 6일
You should not define them in different variables, you should define them into a 3D matrix if the dimensions are same or a cell array if dimensions are different.
A = zeros(1,3,2) ;
for i = 1:2
A(:,:,i) = rand(1,3) ;
end
Now A can be easily used to loop or for nay other calculations.
댓글 수: 0
Guillaume
2017년 9월 6일
편집: Guillaume
2017년 9월 6일
Because I haven't understood past answers to a similar question
The only similar question I can find that you've asked is this one. You were clearly told to use indexing.
I need in any case to obtain as output different variables whose name contains the subscript/index
I'm afraid what you need is to change your way of thinking and adapt to the way matlab (and many other languages) work. You want to apply the same operation to many things. In that case, these many things belong to the same container (ND matrix, cell array, whatever, depending on your particular case). Applying the same operation is then a simple matter of looping over that container. So, in other words: use indexing!
Storing these many things in different variables is a fool's errand. It will make your code a lot harder to debug. It will make it slow. It will be harder to maintain, and most importantly, you'll find that by using indexing you'll end up with a much simpler code.
So, if all your vectors are the same size, store them as a 2D matrix:
something = [1 2 3; ...your first vector
4 5 6; ...your 2nd vector
%etc..
];
and calculating the square of all of these is trivial:
somethingsquared = something .^ 2;
If the vectors are different size, store them in a cell array:
something = {[1 2 3], [4 5 6 7], ... etc
};
calculating the square is a bit more complicated:
somethingsquared = cellfun{@(v) v.^2, something, 'UniformOutput', false);
or
somethingsquared = cell(size(something));
for idx = 1:numel(something)
somethingsquared{idx} = something{idx} .^ 2;
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!