If I write a random matrix (A) and get results:
ans = 1 2 3 4 %next row 5 6 7 8
how can I get it written in this form:
A = [1,2,3,4;5,6,7,8]; ?
(Of course I want to avoid retyping or copy-pasting it)
If I understand your question correctly, mat2str is what you are looking for. Although it won't use commas, but spaces, and overwrite ans (i.e. ans will be of type char afterwards).
Example (the second argument limits the number of digits):
>> rand(2,3); mat2str(ans,2)
ans =
[0.42 0.79 0.66;0.92 0.96 0.036]
The last answer that you calculated is saved in a special variable named ans. Simply assign that value to A.
% some calculations
[1,2,3,4;5,6,7,8]
% assign to A
A = ans;
Related
I'm trying to write a GUI for several purposes, that looks like this,
one of them is create a matrix based on user input via a uitable, so in the untitled_OpeningFcn it was predefined the size and enabled the cell edition
set(handles.uitable1,'Data',cell(2,3));
set(handles.uitable1,'ColumnEditable',true(1,3))
inside the pushbutton2_Callback, I try to read the data written in the table, store it and display in a static text field:
value=get(handles.uitable1,'Data');
value=str2double(value);
set(handles.text2,'String',num2str(value));
But when it is running the text field says "NaN", so the data is not being saved.
What else it is needed?
UPDATE
Thanks by the aswer it does not give the expected results
When using the original code it gives the next output
and when using the suggeste mat2str I got this
I believe the reason for your problem is that the line
value = get(handles.uitable1,'Data');
doesn't result in anything that is convertible to a number.
Consider the following example:
hF = uifigure(); % works the same way with `figure()`.
hT = uitable(hF,'Data',[1 2 3; 4 5 6]);
Then, consider the following:
>> hT.Data
ans =
1 2 3
4 5 6
>> class(hT.Data)
ans =
'double'
>> str2double(hT.Data)
ans =
NaN
>> mat2str(hT.Data)
ans =
'[1 2 3;4 5 6]'
In conclusion - what you need is likely mat2str.
I have 6 different data sets from sensor and I want to find out the Root mean Square error but of the limited signal with limits means RMSE of sig_diff_lim.
I have tried to apply loop but it’s not working can anybody tell me where I am at fault.
Thanks in anticipation.
clc
drv(1)=load('a.mat');
drv(2)=load('b.mat');
drv(3)=load('c.mat');
drv(4)=load('d.mat');
drv(5)=load('e.mat');
drv(6)=load('h.mat');
for i= 1:numel(drv)
t=drv(i).T;
ref=drv(i).P;
lws=drv(i).SWA;
sig_diff(i,:) =lws(i)-ref(i);
swvel_thres=10;
vehvel=30;
SAmax=90;
sig_diff_lim(i,:)=sig_diff((lws(i)<SAmax)&(lws(i)>-SAmax)&(swav(i)<swvel_thres)&(vel(i)>vehvel));
square_error(i,:) = (sig_diff_lim(i)).^2;
mse(i,:)= mean(square_error(i));
rmse(i,:) = sqrt(mse(i));
end
rmse
mse
It seems to me that while assigning to sig_diff_lim(i,:) requires a row vector of size 1*n, the matrix that is returned by
vec=sig_diff((lws_7(i)<SAmax)&(lws_7(i)>-SAmax)&(swav(i)<swvel_thres)&(vel(i)>vehvel))
is of a different size.
Edit
First, are you sure that sig_diff((lws(i)... shouldn't be sig_diff(i,:)((lws(i).... on the assignemnt line?
Now, try element-wise multiplication (operator .* ) between sig_diff (or sig_diff(i,:)), depending on your answer to my previous point) with your logical indexing, like this:
sig_diff.*((lws(i)<... %%%% instead of sig_diff((lws(i)<...
%%% or
sig_diff(i,:).*(lws(i)<...
I think that should do the trick.
Regardless of whether or not this worked, try this example to understand what is going on with that sort of logical indexing:
>> t=[1 2 3 5 7;1 5 6 8 10];
>> t(1,:)(l<7 & l>-7)
ans =
1 2 3 5
>> t(1,:)(l<7 & l>-2)
ans =
1 2 5
Of course, in this example, t is fixed while the thresholds are changing , while in your code the matrix itself is changing. But it shows you that your indexing will return matrices of variable length.
But, with the solution I proposed above,
>> t(1,:).*(l<7 & l>-2)
>> t(1,:).*(l<7 & l>-7)
are both of size 5*1.
Below is the sample code that describe my issue.
ff= [{'1 2 3 4 5'};{'2 2 3 4 2'};{'3 2 3 4 3'};{'4 2 3 4 4'}];
YY=[];
for i=1:length(ff)
xx=str2num(ff{i,1});
YY=[YY;xx];
end
similar to the sample code my real length of ff length is very large and it is taking longer to finish the conversion. is there a way to make it faster?
Your solution is going to be particularly slow since you keep expanding the size of YY every time through the for loop.
To optimize this, you could first convert your cell array of strings into one long string using strjoin. Then you can apply str2num to this entire string at once and reshape the result.
YY = reshape(str2num(strjoin(ff)), [], numel(ff)).'
% 1 2 3 4 5
% 2 2 3 4 2
% 3 2 3 4 3
% 4 2 3 4 4
If your version of MATLAB doesn't have strjoin, you can always replace it with sprintf
YY = reshape(str2num(sprintf('%s ', ff{:})), [], numel(ff)).';
Another option would be to convert each entry of the cell array to numbers using cellfun and num2str and then concatenate the result along the first dimension.
values = cellfun(#num2str, ff, 'UniformOutput', false);
YY = cat(1, values{:});
The first option is about twice as fast since you call num2str only once and the memory needed to store the temporary string created by strjoin is going to be less than the space required to store the same data as a numeric datatype (double).
For example, Why are 1:5 and [1:5] the same in matlab?
What is the reason behind this convention?
The reason for the convention is hard to tell without asking the creators of MATLAB, but here's a little insight. I apologies if this is a bit messy.
If you don't bother reading it all, here's the executive summary:
: is used to create regularly spaced vectors, while square brackets are used for concatenating.
First, you should know that even scalars are considered to be matrices in MATLAB. Scalars are simply 1x1 matrices, or to be more specific: 1x1x1x1x......1. There are in theory infinite amount of trailing singleton dimensions.
1 == [1] == [[[1]]]
Also:
a = 1;
a(1,1,1,:,:,1) %% Messy indexing showing how you can index a matrix using more dimensions than it appears to have.
ans =
1
The documentation says:
The colon operator is used to create regularly spaced vectors (and subscript arrays, and specify for iterations).
As scalars can be created without brackets, there's no reason you should need brackets around a clearly and unambiguously defined operator.
Brackets [] on the other hand creates vectors or matrices by concatenating values and vectors. From the documentation:
Square brackets are used in array construction and concatenation, and also in declaring and capturing values returned by a function.
Therefore, you can basically put brackets around anything you want. The same example as with the scalar above:
1:4 == [1:4] == [[[1:4]]]
Or around cells (doesn't change anything):
a = {3,1:4,'Hello, World!'}
a =
[3] [1x4 double] 'Hello, World!'
b = [a]
b =
[3] [1x4 double] 'Hello, World!'
Concatenating strings:
str1 = 'Hello';
str2 = ', World!';
str = [str1 str2]
str =
Hello, World!
Concatenating vectors:
[1:4, 6:8, 10]
ans =
1 2 3 4 6 7 8 10
If you had to put brackets around the 1:4 part, this would be (also works, but much more cumbersome):
[[1:4], [6:8], 10]
ans =
1 2 3 4 6 7 8 10
A possible reason for the convention:
It would be inconsistent if you needed brackets around 1:3.
Unrelated: For anyone used to other programming languages, brackets inside brackets often means you "go up" one dimension. Therefore, this might be confusing to some.
Well in a way they are and are not the same , In MATLAB every thing is a matrix , even
a = 5
is 1x1 matrix , for 1-D matrix or vector they will perform the same operation
when you write
>> x=1:3
ans =
1 2 3
MATLAB consider the matrix as one dimensional , like for instance in the example below it consider only the last row of the declaration
>> x=1:3;2:4
ans =
2 3 4
but when you specify brackets it can be an nxm dimensional matrix
>> x=[1:3;1:3]
x =
1 2 3
1 2 3
I have two expressions in MATLAB that represent a 365x24 matrix. The first expression has 10, 365x24 matrices and is therefore
PV_power_output(:,:,K)
and the second expression which is again 365x24 but with three possible matrices therefore is
WT_energy_supply(:,:,M);ode here
Now, I am looking to create a third matrix that adds the elements in the same position above and thus form a 365x24 matrix. However I want a set of matrix with all possible combinations of the two expressions shown above (therefore this matrix must be 365x24x30.
How do I go about this?
What about the bsxfun function in MATLAB?
Expand the original matrices (which for clarity I name a and b) with repmat and then just add them, bsxfun is not needed.
repmat(a,[1 1 size(b,3)]) + repmat(b,[1 1 size(a,3)]))
Update
>> size(a)
ans =
364 24 10
>> size(b)
ans =
364 24 3
>> c=repmat(a,[1 1 size(b,3)])+repmat(b,[1 1 size(a,3)]);
>> size(c)
ans =
364 24 30
It looks fine to me. Of course you'll have to replace my variables a and b with your variables PV_power_output and WT_energy_supply.