Matlab, save output in array , loop - arrays

I want to create an array that can store the outputs each time that doing a loop. I think the problem is because in a every new iteration the numbers starts counting from the beginning so it stores only the last iteration! In each iteration the output is an array(7x3) so in total I have to have (28,3).But I tried a lot and i AM GETTING AN ARRAY (28,3) all with zeros except the last 7 rows.
Thank you very much
You can see the code below:
for t=1:ncell % in my case I have 4 cells
ti=sort(T,2)
tt= sort(Cell{t}.ExBot,2)
tq= sort(Cell{t}.ExTop,2)
te= sort(Cell{t}.ExBT,2)
%k=0
z=0
cc=[]
%%%%% for exbottom
I=ones(size(ti,1),1);
for j=1:size(tt,1)
for i=1:size(ti,1)
if tt(j,:)==ti(i,:)
k=k+1 ;
%c(k,:)=[ti(j,:), ti(j+1,:)]
I(i)=0;
cc(k,:)=Y(i,:);
cc(size(tt,1)+1,:)=cc(1,:)
else
end
end
end
end

Although more info would help as mentioned in the comments, from the information you've given, the problem is most likely in setting cc to empty when you start processing each cell.
cc=[];
On exiting the outermost loop you will only have results for the last iteration.
On a related note you may want to use isequal or all for the comparison of vectors i.e. if isequal(tt(j,:),ti(i,:))

Related

Display content of cell array corresponding to another cell array with Matlab

I have a dataset stored in SUBJ{s}.,and then fields SUBJ.WORDS{w} and SUBJ.RECALL{r}. I tried to create a loop that finds when SUBJ.RECALL corresponds to 1 (1=word remembered, 0 not remembered). After that I want to have displayed the words that correspond to the positions where SUBJ.RECALL is 1. Say
SUBJ{1}.WORDS{1}={‘Apple’, ‘Melon’, ‘Cheese’ ,’Pancakes’,Tomatoes’}% words presented.
`SUBJ{1}.RECALL{1}=[1 0 0 1 1]% 1=word recalled 0=word non recalled.
What I want is to display the words that have been recalled, meaning the words that correspond to 1 in SUBJ.RECALL.
I have done this:
for s=1:length(SUBJ)
for w=1:length(SUBJ.WORDS)
for r=1:length(SUBJ.RECALL)
if SUBJ{s}.RECALL{r}==1
disp(SUBJ{s}.WORDS{(SUBJ.RECALL{r}==1)})
end
end
end
end
Error: Attempt to reference field of non-structure array.
for s=1:length(SUBJ)
for w=1:length(SUBJ.WORDS)
for r=1:length(SUBJ.RECALL)
find(SUBJ{s}.RECALL{r}==1)
disp(SUBJ{s}.WORDS{(SUBJ.RECALL{r}==1)})
end
end
end
Error: Attempt to reference field of non-structure
Thanks in advance for any comment!
You probably do not need this many for-loops, as you can process the cell element contents using find. I included a sample code below. This works for your sample data, but might fail with whole dataset as i don't know the structure.
for s=1:length(SUBJ)
% Store in variables to make code more readable
words = SUBJ{s}.WORDS{1};
hits = SUBJ{s}.RECALL{1};
if max(hits) == 1 % Only print when hits
disp(words(find(hits)))
end
end

How do I subtract a value from each element of an array in Lua?

I am trying to write a function to find the variance of a data set.
I am stuck on a small problem. I have an array, and I want to find how far each element in the array is from the average. Here is a simplified version of what I wrote:
>y={1,2,3}
>y_average=2
>y_diff={}
>for key, value in pairs(y) do y_diff[key]=(y[key]-y_average)
>>return unpack(y_diff)
>>end
-1
what I want to get: -1, 0, 1
Why does it only give me the first value and not all three?
Your return is breaking the loop in the first iteration as mentioned in a comment. Try this:
for i in ipairs(y) do
y_diff[i] = y[i] - y_average
end
print( table.concat(y_diff, '\t') )
table.concat doesn't have a limit on the amount of elements it can handle and it would be what you would use if you wanted to put these elements in a file faster than writing them one by one.

How do I account for the extra elements at the end of an array if it is shorter in the next iteration of a nested loop

I've got a nested for loop, in the inner loop I've got an array that will change size and value in each iteration,e.g;
a=[ 2 3 4]
and in the next iteration it will be :
a=[9 5]
but the result of my code is :
a=[9 5 4]
a(3) is the problem, it is from the previous iteration and I don't want it,so what should I do?
I do not know how to write my code here cause it contains lots of functions and you wont understand it!?
but it's sth like this:
for j=1: 5
%l is the length of row in cell array(a) that varies from one row to another
for i=1:l
dn=a{j,i};
spp(t)=dn(1)
end
targ{j,1}=spp;
end
spp is the problem here
Insert a clear command to delete the temporary variable (once spp have three elements, it never goes back to a 2 elements vector unless you clear it or declare it).
...
targ{j,1}=spp;
clear spp;
...
Alternatively, you can code the matlab-way by declaring your variable before it gets populated. In this situation, there is no need for a clear command.
for j=1:5
%l is the length of row in cell array(a) that varies from one row to another
spp = zeros(1,l);
for i=1:l
...

Outputting the rows from an (ixj) array into individual (5xj/5) arrays in a text file

In a program I'm writing, I've created an allocated, final product array AFT(n,92). In my output I would like present each row as its own table, 5 columns wide.
So in this case, it would be n individual tables of 19 rows X 5 columns with only 2 values on the final row. I attempted doing this as a do loop as shown in the code snip below, but the output comes out as just one long column. I'm not sure where to go from here.
DO i=1,n
WRITE(4,800) t(i), ' HHMM LDT' !Writes the table header using an array which holds the corresponding time value
800 FORMAT(14, A9)
DO j=1,92
WRITE(4,900) AFT(i,j)
900 FORMAT(5ES23.14)
END DO
END DO
I believe this is happening because the write command is performed for each j individually due to the use of a loop, but my inexperience with FORTRAN is leading me to a blank when I try to come up another approach.
Yes, each write statement produces one line of text output. If you want multiple items to be included in the same output record, you have to include them in the write statement. If you want to include portions of an array, you can use techniques such as:
do i=1, N
write (*, *) (array (i,j), j=1, 5)
end do
or
do i=1, N
write (*, *) array (i, 1:5)
end do
The first is using implied do loops, the second array sections.

create an new cell array everytime a loop cycles through in matlab

I am new to MatLab but I have some experience with C#. I have a large dataset <169360x97> that I need to break up into 464 cell arrays. I currently have a loop that will cycle through the dataset and make a cell array, but I can not figure out how to have the loop create a new cell array every time instead of just rewriting over the same data. Here is the loop I have written.
b=5476;
e=5840;
while(b<169360)
dataset2cell(JeaAddressKwh(b:e,1:97));
b=e+1;
e=e+365;
end
I have tried the following, but i get an error message every time:
n=16;
b=5476;
e=5840;
while(b<169360)
n=dataset2cell(JeaAddressKwh(b:e,1:97));
n+1;
b=e+1;
e=e+365;
end
So basically what I am trying to get as an output is a different cell array called 16 through 464. I would appreciate any help. Thanks.
In the first loop you are not saving the cell array and in the second loop you over overwriting the previous cell array and trying to add 1 to it, without saving the result.
Try something like this:
n=cell(16,1);
b=5476;
e=5840;
i = 1;
while(b<169360)
n{i}=dataset2cell(JeaAddressKwh(b:e,1:97));
i = i+1;
b=e+1;
e=e+365;
end

Resources