Little Bit Help Required Regarding Loop
1 次查看(过去 30 天)
显示 更早的评论
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
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
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
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
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 Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!