Little Bit Help Required Regarding Loop

조회 수: 1 (최근 30일)
John Hock
John Hock 2019년 2월 17일
댓글: Stephen23 2019년 2월 18일
Hi EveryOne
I am currently working on this code
a=[1 2 3]
b=[3 4 5]
c=[34 5 6 ]
for i=1:1:3;
C = {[a];[b];[c]};
d= cellfun(@(v)v(i),C)
end
And the output is
d =
1
3
34
d =
2
4
5
d =
3
5
6
Every time loop runs it updated the last results.
I just want to save all the values means every time when loop run it keep save the last results and save the new results in the new coloumn
When I use
d(i)= cellfun(@(v)v(i),C)
The code give error
In an assignment A(I) = B, the number of elements in B and I must be the same.
Please help in this matter
Thanks
Regards
  댓글 수: 1
Stephen23
Stephen23 2019년 2월 17일
편집: Stephen23 2019년 2월 17일
Note that these square brackets are totally superfluous:
C = {[a];[b];[c]};
All you need is:
C = {a;b;c};
You will notice that these superfluous square brackets are underlined by the MATLAB Editor:
Also there is no need to completely redefine C on every loop iteration: it is simpler and more efficient to just define it once before the loop.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2019년 2월 17일
편집: Stephen23 2019년 2월 17일
"I just want to save all the values means every time when loop run it keep save the last results and save the new results in the new coloumn"
Why so complex? One simple concatenation gives exactly the same result:
>> d = [a;b;c]
d =
1 2 3
3 4 5
34 5 6
Or, if your arrays are already in a cell array and you just need to concatenate them together:
>> tmp = {a;b;c}; % your cell array
>> d = vertcat(tmp{:})
d =
1 2 3
3 4 5
34 5 6
If you really want to use a slow and complex loop to do this:
>> d = nan(3,3); % preallocate
>> for k = 1:3, d(:,k) = [a(k),b(k),c(k)]; end
>> d
d =
1 2 3
3 4 5
34 5 6
which could also be complicated even more with a cellfun call:
>> d = nan(3,3);
>> for k = 1:3, d(:,k) = cellfun(@(t)t(k),tmp); end
>> d
d =
1 2 3
3 4 5
34 5 6
  댓글 수: 2
Stephen23
Stephen23 2019년 2월 18일
John Hock's "Answer" moved here:
@Stephen Cobeldick
Thanks alot sir for your time and help
Let me explain you a little bit more
I had a data from 26 channles of EEG
Lets assume 2560 points for each chanenl
i just want to read the 1st element of each channel and put it in a array
and then second element till 2560 element .
Will cancatenation is ok for this work?
And 1 more thing will you please help me a little bit more that after reading first element of all channels i also want to find minimum value,maximum value and their mean also.and the same for rest values
Thanks in advance
Stephen23
Stephen23 2019년 2월 18일
@John Hock: assuming that each channel has exactly the same number of data points, then your best approach would be to concatenate them all into one numeric array. Then you can trivially call min, max, mean etc. with their optional dimension argument.
For example, where tmp is a cell array of identically-sized row vectors:
d = vertcat(tmp{:});
mean(d,1)
max(d,[],1)
min(d,[],1)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2013a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by