Octave read each line of file to vector - file

I have a file data.txt
1 22 34 -2
3 34 -3
2
3 43 -3 2 3
And I want to load this file onto Octave as separate matrices
matrix1 = [1; 22; 34 ;-2]
matrix2 = [3; 34 -3]
.
.
.
How do I do this? I've tried fopen and fgetl, but it seems as if each character is given its own spot in the matrix. I want to separate the values, not the characters (it's space delimited).

quick and dirty:
A = dlmread("file");
matrix = 1; # just for nice varname generation
for i = 1:size(A,1)
name = genvarname("matrix",who());
eval([name " = A(i,:);"]);
eval(["[_,__," name "] = find(" name ");"]);
end
clear _ __ A matrix i
The format needs to be as you specified.

Related

Delete values between specific ranges of indices in an array

I have an array :
Z = [1 24 3 4 52 66 77 8 21 100 101 120 155];
I have another array:
deletevaluesatindex=[1 3; 6 7;10 12]
I want to delete the values in array Z at indices (1 to 3, 6 to 7, 10 to 12) represented in the array deletevaluesatindex
So the result of Z is:
Z=[4 52 8 21 155];
I tried to use the expression below, but it does not work:
X([deletevaluesatindex])=[]
Another solution using bsxfun and cumsum:
%// create index matrix
idx = bsxfun(#plus , deletevaluesatindex.', [0; 1])
%// create mask
mask = zeros(numel(Z),1);
mask(idx(:)) = (-1).^(0:numel(idx)-1)
%// extract unmasked elements
out = Z(~cumsum(mask))
out = 4 52 8 21 155
This will do it:
rdvi= size(deletevaluesatindex,1); %finding rows of 'deletevaluesatindex'
temp = cell(1,rdvi); %Pre-allocation
for i=1:rdvi
%making a cell array of elements to be removed
temp(i)={deletevaluesatindex(i,1):deletevaluesatindex(i,2)};
end
temp = cell2mat(temp); %Now temp array contains the elements to be removed
Z(temp)=[] % Removing the elements
If you control how deletevaluesatindex is generated, you can instead directly generate the ranges using MATLAB's colon operator and concatenate them together using
deletevaluesatindex=[1:3 6:7 10:12]
then use the expression you suggested
Z([deletevaluesatindex])=[]
If you have to use deletevaluesatindex as it is given, you can generate the concatenated range using a loop or something like this
lo = deletevaluseatindex(:,1)
up = deletevaluseatindex(:,2)
x = cumsum(accumarray(cumsum([1;up(:)-lo(:)+1]),[lo(:);0]-[0;up(:)]-1)+1);
deleteat = x(1:end-1)
Edit: as in comments noted this solution only works in GNU Octave
with bsxfun this is possible:
Z=[1 24 3 4 52 66 77 8 21 100 101 120 155];
deletevaluesatindex = [1 3; 6 7;10 12];
idx = 1:size(deletevaluesatindex ,1);
idx_rm=bsxfun(#(A,B) (A(B):deletevaluesatindex (B,2))',deletevaluesatindex (:,1),idx);
Z(idx_rm(idx_rm ~= 0))=[]

Storing blocks of vector data into an array in Matlab

I have an array in Matlab
A = [1 2 3 4 5 6 7 8 9;
67 67 67 86 86 86 86 67 67]';
where every point in the first row of A corresponds to a "code" either 67 or 86. I am trying to extract these blocks of "67s" and "86s" such that every time a block starts the corresponding elements are put into the 3rd dimension of a different array called X, where the .
So for e.g. in A I have 3 different blocks, so I would like to end up with an array X of size 1x9x3. And for e.g. the first 67 block I would like to have X
X(1,:,1) = [1 2 3];
I understand that I would "fill up" this vector X using a for loop
for i=1:size(A,2)
for j=1:size(A,2) %actually j should be up till the number of blocks present
X(1,i,j) = A(1,i)
end
end
But this isn't correct or complete of course because firstly I'm unsure how to separate out the "blocks" and how to correctly "fill in" the j's in X(1,i,j). Secondly how can I get the code to recognise how many blocks there are?
Can anyone help?
Thanks
One possible approach, based on this answer:
>> B = accumarray([0; cumsum(diff(A(:,2)) ~= 0)] + 1, A(:,1), [], #(x) {x}, [])
Now you have this:
>> B{1}
ans =
1
2
3
>> B{2}
ans =
4
5
6
7
>> B{3}
ans =
8
9

How to load the multiple data to x by x array in MATLAB?

I have a bunch of .txt text files. this file is included like this
e.g.
text1.txt-
1
44
22
100
..
..
text2.txt-
7
14
82
50
..
..
....
text260.txt-
31
4
62
700
..
..
like this, text#.txt have almost 260 files.
I want to load into MATLAB with array like this.
a =
1 7 ... 31
44 14 ... 4
22 82 ... 62
100 50 ... 700
.. .. ... ..
.. .. ... ..
As I know, the load() function is in MATLAB. But I don't know how to load into array format.
How to load the multiple data to x by x array in MATLAB ?
You would have to slowly build up the array. My first thought would be to do this:
arr = [];
for i = 1:260
arr = [arr; load(['text' num2str(i) '.txt'])'];
end
You start off with an empty array, and then iterate through the files, appending each file to the array. NB, if the final array is quite large you might want to consider a different approach involving pre-allocating the array:
arr = zeros(length(load('text1.txt')), 260);
for i = 1:260
arr(:,i) = load(['text' num2str(i) '.txt'])';
end

how to convert a number array to a single string in matlab

How can i convert a numeric array to a single string, where array numbers will be separated with a comma ?
(e.g. convert A=[1 2 3] to a string '1,2,3')
Moreover, is there any way to apply the same above in case that matrix A contains variables in a for loop?
(e.g.
for i=1,10
A(i)=[1 1 i+1];
end
As variable i varies, I need to obtain a string '1,1,i+1'
thanks a lot !
There is a num2str() function
>> test =[123 124 125] % 3 element vector
test =
123 124 125
>> num2str(test) % 1 element string
ans =
123 124 125
and also a function to write ASCII delimited files
the process can easily be reversed with the str2num function, as dan pointed out
I think you need this:
for i=1:10
disp(['1,1,',num2str(i+1)])
end
Note: Try to avoid 'i' as the iteration variable.
The output:
1,1,2
1,1,3
1,1,4
1,1,5
1,1,6
1,1,7
1,1,8
1,1,9
1,1,10
1,1,11
for i=1:10
s = sprintf('%d,', A);
S{i} = s(1:end-1);
end
The function mat2str does just that:
>> A = [1 2 3];
>> mat2str(A)
ans =
[1 2 3]

find and replace values in cell array

I have a cell array like this: [...
0
129
8...2...3...4
6...4
0
I just want to find and replace specific values, but I can't use the ordinary function because the cells are different lengths. I need to replace many specific values at the same time and there is no general function about how values are replaced. However, sometimes several input values should be replaced by the same output.
so I want to say
for values 1:129
'if 0, then 9'
'elseif 1 then 50'
'elseif 2 or 3 or 4 then 61'
etc...up to 129
where these rules are applied to the entire array.
I've tried to work it out myself, but still getting nowhere. Please help!
Since your values appear to span the range 0 to 129, one solution is to add one to these values (so they span the range 1 to 130) and use them as indices into a vector of replacement values. Then you can apply this operation to each cell using the function CELLFUN. For example:
>> C = {0, 129, [8 2 3 4], [6 4], 0}; %# The sample cell array you give above
>> replacement = [9 50 61 61 61 100.*ones(1,125)]; %# A 1-by-130 array of
%# replacement values (I
%# added 125 dummy values)
>> C = cellfun(#(v) {replacement(v+1)},C); %# Perform the replacement
>> C{:} %# Display the contents of C
ans =
9
ans =
100
ans =
100 61 61 61
ans =
100 61
ans =
9

Resources